Ok, it used to be super easy with matrix but now that Matrix is being depreciated I'm not sure how to set the state of a radio button...
I've setup the radio button these ways:
IBOutlet NSButtonCell *pending;
IBOutlet NSButton *pending;
__weak IBOutlet NSButton *pending;
But the code below doesn't do anything:
if([p.status isEqualToString:@"Pending"]){
pending.state = 1; //Doesn't work
[NSButtonCell:pending [setstate:NSOnState]]; //Doesn't work
[(NSButtonCell *)pending.cell setstate(ON)]; //Doesn't work
}
There has to be a way to do it and I've searched high and low... Any help would be seriously appreciated.
Thanks in advance.
I think I know what your problem is. Did you connect those radio button outlets right to the row in the table view? That won't work because outlets are to-one relationships and cannot be used to point to multiple rows at the same time (And, most likely, the one reference you have is likely for some "prototype" row that isn't actually in the table.)
So to fix this, I'd recommend getting rid of those outlets. Open up IB and use the Identity inspector (⌥⌘3) to set custom identifiers for the three radio buttons. Then you could write code like this inside your if (p) { block (starting at line 9) to process each row's set of buttons:
for (NSView *subview in cell.subviews) {
if (![subview isKindOfClass:[NSButton class]]) continue;
if ([subview.identifier isEqualToString:@"PendingButton"])
[(NSButton *)subview setState:[p.status isEqualToString: @"Pending"] ? NSOnState : NSOffState];
else if ([subview.identifier isEqualToString:@"ApprovedButton"])
[(NSButton *)subview setState:[p.status isEqualToString: @"Approved"] ? NSOnState : NSOffState];
else if ([subview.identifier isEqualToString:@"RejectedButton"])
[(NSButton *)subview setState:[p.status isEqualToString: @"Rejected"] ? NSOnState : NSOffState];
}
Notice that all of the buttons are covered and that any that shouldn't be selected are explicitly disabled. This covers any artifacts left over from if table rows are resused.
You might even want to use the exact values of the Proposal's status property. Then you could write it in an even simpler way:
for (NSView *subview in cell.subviews) {
if ([subview isKindOfClass:[NSButton class]])
[(NSButton *)subview setState:[p.status isEqualToString: subview.identifier] ? NSOnState : NSOffState];
}
Just be careful to make sure they match exactly or it won't work.
Edit: Fixed the fast-enumeration declarations in line 1 (I had accidentally used the Swift form, not the Obj-C form)