views:

95

answers:

3

I have an NSSegmentedControl with two segments set to "Select None" mode in Interface Builder. No matter what I try, I can't get -selectedSegment to return anything but 0, even though segment 0 is even disabled by default and can't possibly be selected. Here's the relevant function that gets called when you click any segment on the control:

-(IBAction)changeStep:(id)sender
{
    [stepContainer setHidden:TRUE];
    [(NSView *)[[wizard stepArray] objectAtIndex:(NSInteger)[wizard step]] removeFromSuperview];
    switch ([[navigationButton cell] selectedSegment])
    {
    case 0:
        [wizard setStep:(NSInteger *)[wizard step]-1];
        break;
    case 1:
        [wizard setStep:(NSInteger *)[wizard step]+1];
        break;
    default:
        break;
    }
    //[[navigationButton cell] setSelected:FALSE forSegment:[navigationButton selectedSegment]];

    if ([wizard step] > 0)
    {
        [wizard setStep:0];
        [navigationButton setEnabled:FALSE forSegment:0];
    }

    NSLog(@"%d", [wizard step]);
    [stepContainer addSubview:(NSView *)[[wizard stepArray] objectAtIndex:(NSInteger)[wizard step]]];
    [stepContainer setHidden:FALSE withFade:TRUE];
}

I've also tried using -isSelectedForSegment, but it has the same result.
Any help you can provide would be awesome, I have no idea what I'm doing wrong. Thanks!
SphereCat1

EDIT: This is a facepalm moment, see my answer below.

A: 

Are you sure that navigationButton is initialised? Put an NSLog statement like this:

NSLog (@"%@", navigationButton);

If you message a nil object, you will get 0 as a result. If navigationButton is an IBOutlet you need to make sure it is properly linked up in Interface Builder.

dreamlax
It is indeed initalized. The NSLog returns <NSSegmentedControl: 0x1002316a0>
SphereCat1
A: 

Alright, this is a facepalm moment. Turns out I was NSLogging the wrong thing, and there's just an error in my step increment/decrement logic.

All better! :)

facepalm
SphereCat1

SphereCat1
+1  A: 

You seem to be doing a lot of casting in places that you shouldn't need to.

[(NSView *)[[wizard stepArray] objectAtIndex:(NSInteger)[wizard step]] removeFromSuperview];

What does [wizard step] return? You should not need to cast it.

    [wizard setStep:(NSInteger *)[wizard step]-1];

This is even more inconsistent. NSInteger * is a pointer type (NSInteger is not an object). If [wizard setStep:] takes a NSInteger *, then it is probably because it's being used as an inout parameter, perhaps to provide you with the new step if it is different from the requested one. In that case, you need to do something like this:

NSInteger newStep = [wizard step] - 1;
[wizard setStep: &newStep];
Nicholas Riley
You're right, that was unnecessary. I think I had a different function originally, and just didn't remove the cast when I changed it. Thanks for pointing that out!
SphereCat1