3

I'm using a TabBar app with a navigation item that includes a UISegmentedControl.

I've connected a method when the event "value changed" is caught.

The method always catch 0 as SegmentIndex...

Here's my header file :

#import <UIKit/UIKit.h>


@interface GraphNavController : UINavigationController {

    IBOutlet UIImage *image;
    CGPoint gestureStartPoint;
    UISegmentedControl *segmentedControl;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;

-(IBAction) segmentedControlIndexChanged;

-(void)journalier;
-(void)mensuel;
-(void)annuel;


@property (nonatomic, retain) IBOutlet UIImage *image;
@property (nonatomic, retain) IBOutlet UISegmentedControl *segmentedControl;

@end

The method is here :

-(IBAction) segmentedControlIndexChanged{

    switch (self.segmentedControl.selectedSegmentIndex) {
        case 0:
            NSLog(@"1");
            break;
        case 1:
            NSLog(@"2");
            break;
        case 2:
            NSLog(@"3");
            break;
        default:
            break;
    }

}

I hope we will find a solution

Thanks a lot

Thunder Rabbit
  • 5,405
  • 8
  • 44
  • 82
clement
  • 4,204
  • 10
  • 65
  • 133

2 Answers2

5

A possible explanation for this problem is that self.segmentedControl is nil. Is self.segmentedControl as an IBOutlet? Or created in code? Check if self.segmentedControl == nil.

fsaint
  • 8,759
  • 3
  • 36
  • 48
  • Thanks for your help. For my case, NSLog(@"self.segmentedControl == %d", self.segmentedControl); return me 0. segmentedcontrol is an Outlet (I've updated my initial post to show the header ^^) – clement Apr 28 '11 at 11:00
  • 0 is nil. The problem **is** that self.segmentedControl is nil. Have you assigned the IBOutlet in Interface Builder? – fsaint Apr 28 '11 at 11:28
  • wich elements do I have to link??? the BarSegmentedControl with what? sorry... I don't made the link with the outlet ! – clement Apr 28 '11 at 12:11
  • I've Linked the Outlet from the GraphNavController to the BarSegmented and it's running... beginner's fault... HUNDRED OF THANKS !!! – clement Apr 28 '11 at 12:35
  • i took unsigned short and store changed segment index in it as it changes. ;) problem solved. – khunshan Sep 02 '14 at 13:13
0

I had the same problem and fight with the bug of nill UISegmentedControl.

AND I FOUND THE ANSWER.

Sometimes you have the same problem but you have all connections in XIB.

THE PROBLEM: You have nill in self.segmentedControl. In most situations when you have everything connected in XIB correct it will also not work.

THE CAUSE: Because of difference in studing the Objective-C we use different styles of writing code.

The Decision: For example we write in interface UISegmentedControl *control; When you @synthesize how would you do that? If you write @synthesize control=_control; you will always have nill when you write like that NSLog(@"%i",control.selectedSegmentIndex).

So we have the mistake in @synthesize or in Code.

Decision

For thou who write @synthesize control; use control.selectedSegmentIndex

For thou who write @synthesize control=_control; use _control.selectedSegmentIndex

And everything will work correct.

Roman Safin
  • 704
  • 7
  • 16