Search code examples
macoscocoaradio-buttonnsbutton

NSButton Radio without Matrix - trouble identifying the buttons and setting state


I have an NSTableView with a column with three radio buttons. I have created them in IB and bound them all to the same action but I can't figure out how to retrieve which radio is checked to call the appropriate action. This is how I used to do it, but again, no more.

-(void) selectedAction:(id) sender{
    NSInteger col = [sender selectedColumn];
    if(col==0){
        proposal.status = @"Pending";
    }else if(col==1){
        proposal.status = @"Approved";
    }else if(col==2){
        proposal.status = @"Rejected";
    }
    [[DBManager getProposalTable] updateWithProposal:proposal]; 
}

I used to be abele to create radio buttons with matrix and easily set the state of the button and retrieve which ratio was checked. With Matrix being depreciated I'm not really sure how to do that anymore. This used to work but not anymore.

if(p){
    if([proposal.status isEqualToString:@"Pending"]){
        [matrix selectCell:[cells objectAtIndex:0]];
    }else if([proposal.status isEqualToString:@"Approved"]){
        [matrix selectCell:[cells objectAtIndex:1]];
    }else if([proposal.status isEqualToString:@"Rejected"]){
        [matrix selectCell:[cells objectAtIndex:2]];
    }
}

Like I said, these radios live inside an NSTableView and are set programmatically based on the value in a DB. When one is checked the value in the DB is updated.

Any advice on how to set a radio button's state and on how to retrieve which one was selected would be much appreciated.


Solution

  • Here's how I was able to identify which button was clicked:

    - (IBAction)selectedAction:(id)sender{
        NSInteger selected = [self->tableView rowForView:sender];
    
        NSString *row = list[selected];
        Proposal *p = list [selected];
    
        NSString *button = [sender title];
    
                if([button isEqualToString:@"Pending"]){
                    p.status = @"Pending";
                }else if([button isEqualToString:@"Approved"]){
                    p.status = @"Approved";
                }else if([button isEqualToString:@"Rejected"]){
                    p.status = @"Rejected";
                }
                [[DBManager getProposalTable] updateWithProposal:p];
            }
    

    I haven't been able to set the button state though :(