Re: Initializing Radio buttons in an NSMatrix problem
Re: Initializing Radio buttons in an NSMatrix problem
- Subject: Re: Initializing Radio buttons in an NSMatrix problem
- From: Fritz Anderson <email@hidden>
- Date: Mon, 01 Aug 2011 08:40:16 -0500
On 1 Aug 2011, at 7:47 AM, John James wrote:
> Using Lion: I have a NSMatrix 1 row 3 cols.
> at Startup I can not get the first radio button to show an indication; works fine if user clicks on any one.
> Relavent code (called from awakeFromNib):
> (framesPersec is an outlet for the NSMatrix with tags 1,2,3 respectively)
>
> - (void) initFramespersec
> {
> if (framesPersec!=NULL) {
> [framesPersec setAllowsEmptySelection: NO];
> indexSelectedItem = 2; <========================== I change this to 1 or 2
> if (indexSelectedItem==1)
> [self setMyFramesPerSec: 24];
> else if (indexSelectedItem==2)
> [self setMyFramesPerSec: 29.97];
> else
> [ self setMyFramesPerSec: 30];
> secPerFrame = 1./myFramesPerSec;
> [self updateInterBeatNumbers];
> NSCell* mycell = [framesPersec cellWithTag: indexSelectedItem];
> [mycell setState:NSOnState];
> }
> }
(Quick note: The way to branch against integral values is a switch statement. It indicates your intentions, gives you a clean way to post an error for an unexpected value, and future-proofs against adding or removing tags.)
I tried this on Lion (abbreviated):
@interface KillMeRadioButtonAppDelegate : NSObject <NSApplicationDelegate>
@property(nonatomic, retain) IBOutlet NSMatrix *buttonMatrix;
- (IBAction)matrixChanged:(id)sender;
@end
@implementation KillMeRadioButtonAppDelegate
// Set only one, or neither, to 1:
#define SET_CELL_DIRECTLY 0
#define SET_THROUGH_MATRIX 0
- (void) awakeFromNib
{
NSLog(@"%s: Initial selection = %ld",
__PRETTY_FUNCTION__, [self.buttonMatrix selectedTag]);
#if SET_CELL_DIRECTLY
NSCell * secondCell = [self.buttonMatrix cellWithTag: 2];
[secondCell setState: NSOnState];
#elif SET_THROUGH_MATRIX
[self.buttonMatrix selectCellWithTag: 2];
#endif
}
- (IBAction) matrixChanged: (id) sender
{
NSLog(@"%s: Selected %ld",
__PRETTY_FUNCTION__, [self.buttonMatrix selectedTag]);
}
@end
What I'm seeing is that the matrix comes into -awakeFromNib with the first cell selected. ([KillMeRadioButtonAppDelegate awakeFromNib]: Initial selection = 1)
If SET_CELL_DIRECTLY is 1 (and SET_THROUGH_MATRIX is 0), both the first cell and the cell with tag 2 are on.
If SET_THROUGH_MATRIX is 1 (and SET_CELL_DIRECTLY is 0), only the cell with tag 2 is on.
So the first lesson is that accessing the cells directly doesn't do what you hope.
The next thing to suspect is that setMyFramesPerSec: or updateInterBeatNumbers is somehow setting the matrix selection. And double-check that the button tags are as you expect.
— F
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden