Question

I've got some code for an UISegmentSwitch but it just don't work.

Code:

- (IBAction)seg:(id)sender {
switch (segSwitch.selectedSegmentIndex) {
    case 0:
        NSLog(@"test1");
        break;

    case 1:
        NSLog(@"Testy");
        break;

    default:
        break;
}
}

Ofcourse everything is declared and connected.

Thanks in advance

Was it helpful?

Solution

Try doing it this way

- (IBAction)seg:(id)sender {

        UISegmentedControl *yourSegCtrl=(UISegmentedControl *)sender;

        switch (yourSegCtrl.selectedSegmentIndex) {
            case 0:
                NSLog(@"test1");
                break;

            case 1:
                NSLog(@"Testy");
                break;

            default:
                break;
        }
}

also make sure that your segmented control is connected to the IBAction

OTHER TIPS

Have you wired your IBAction to the "value did change" slot, or the "touch up inside" ? You need to register for "value did change" for UISegmentedControls.

I've never heard of a UISegmentSwitch, but I'll assume that you're talking about a UISegmentedControl.

Try putting a break point at the switch statement to see if the seg: method is getting called. It it's not, then you haven't properly connected the method to the UISegmentedControl. That is easily fixed, but you'll have to show some more code.

You should use sender.selectedSegmentIndex instead of segSwitch.selectedIndex. NSLog the value before the switch statement (or check it in the debugger) to see if you're getting values you expect.


There are a few possibilities why the method could not be called. I think that the one suggested by Cyrille is a likely one.

First of all, make sure that your control is actually getting touches. Does the selected segment change when you tap on the different segments? If not, than you control is probably not getting touches. Check to make sure that userInteractionEnabled is set to YES for the control and its superviews.

Secondly, select your segmented controller in the storyboard, open up the Xcode inspector window, and select the last (6th or rightmost) tab at the top of the inspector window. Here you'll see what event has been hooked up to what method. The send event should be Value Changed and it should be wired to File's Owner seg:.

You could also check to make sure that File's Owner is set to the correct View Controller.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top