سؤال

I create radiobutton has 2 row and 1 column and set action for it but it work not correct, it always seleced cell tag =0. This is my codes:

       NSButtonCell *prototype= [[NSButtonCell alloc] init];
       [prototype setTitle:@"Normal"];
        [prototype setButtonType:NSRadioButton];
        NSRect matrixRect = NSMakeRect(170, 130, 150, 125.0);
        NSMatrix *myMatrix = [[NSMatrix alloc] initWithFrame:matrixRect
                                                        mode:NSRadioModeMatrix
                                                   prototype:(NSCell *)prototype
                                                numberOfRows:2
                                             numberOfColumns:1];
        [myMatrix setAction:@selector(radioButtonClicked:)];
        [myMatrix setTarget:self];
        [guiView addSubview:myMatrix];
        NSArray *cellArray = [myMatrix cells];
        //[[cellArray objectAtIndex:0] setTitle:@"Normal"];
        [[cellArray objectAtIndex:1] setTitle:@"Mute"];
        [prototype release];
        [myMatrix release];

- (IBAction)radioButtonClicked:(NSMatrix * )sender {
    //thoahuynh - check selected tag of radio button
    NSInteger tag = [[sender selectedCell] tag];
    switch ( tag ) {
        case 0:
            NSLog(@"Selected Normal");
            break;
        case 1:
            NSLog(@"Selected Mute");
            break;
    }}

Can you help me? Thanks in advance

هل كانت مفيدة؟

المحلول 2

You need to set a tag to each button.

enter image description here

نصائح أخرى

myMatrix does not have a tag.

myMatrix has to have a tag.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top