Ashish Agarwal
Ashish Agarwal

Reputation: 14925

UISegmentedControl won't change value

I am trying to create a UISegmentedControl programatically. I have a UIViewController in the storyboard with nothing in it.

.h file

UISegmentedControl *segmentedControl;
NSString *feedBackButtonTitle;
NSString *contactsButtonTitle;

and here are the property declarations.

@property (nonatomic,retain) IBOutlet UISegmentedControl *segmentedControl;
-(void) segmentedControlIndexChanged;

In viewDidLoad: I have initalized and added the UISegmentedControl.

   NSString *language = [[NSLocale preferredLanguages] objectAtIndex:0];
if ([language isEqualToString:@"en"]){
    contactsButtonTitle = [[[configFileDictionary objectForKey:@"Contacts"] objectForKey:@"Label"] objectForKey:@"en"];
    feedBackButtonTitle = [[[[[configFileDictionary objectForKey:@"Contacts"] objectForKey:@"Contact"]objectForKey:@"Feedback"]objectForKey:@"Label"]objectForKey:@"en"];
}
else if([language isEqualToString:@"fr"]){
    contactsButtonTitle = [[[configFileDictionary objectForKey:@"Contacts"] objectForKey:@"Label"] objectForKey:@"fr"];
    feedBackButtonTitle = [[[[[configFileDictionary objectForKey:@"Contacts"] objectForKey:@"Contact"]objectForKey:@"Feedback"]objectForKey:@"Label"]objectForKey:@"fr"];

}
NSArray *itemsArray = [[NSArray alloc] initWithObjects:contactsButtonTitle, feedBackButtonTitle, nil];
segmentedControl = [[UISegmentedControl alloc] initWithItems:itemsArray];
segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
//segmentedControl.selectedSegmentIndex = 0;
segmentedControl.frame = CGRectMake(0.0f, 0.0f, 320.0f,40.0f);
[segmentedControl addTarget:self action:@selector(segmentedControlIndexChanged) forControlEvents:UIControlEventValueChanged];

 //  [self.view addSubview:segmentedControl];

// Create view for contact display.
[self createViews];

and this the (void)segmentedControlIndexChanged

-(void)segmentedControlIndexChanged
{
    switch (self.segmentedControl.selectedSegmentIndex)
    {
        case 0:
            [self createViews];
            break;
        case 1:
            [self showFeedbackForm];
            break;
        default:
            break;
    }
}

This is showing the segmented control on the screen just fine, but when I click on the options in the segmentedcontrol, it always goes into the option case 0. segmented control and opens [self createViews];

On inserting breakpoints at the line case 0, I noticed that the option _selectedSegment in segmentedControl is 1. This doesn't make any sense.

Upvotes: 0

Views: 2545

Answers (2)

Alladinian
Alladinian

Reputation: 35616

Ok a few things here:

  1. Since you got a property, use self.segmentedControl instead of segmentedControl
  2. With a simple change in your selector signature life is just easier:

[self.segmentedControl addTarget:self 
                          action:@selector(segmentedControlIndexChanged:)
                forControlEvents:UIControlEventValueChanged]; // Notice the colon

-(void)segmentedControlIndexChanged:(id)sender
{
    switch ([sender selectedSegmentIndex])
    {
        case 0:
            [self createViews];
            break;
        case 1:
            [self showFeedbackForm];
            break;
        default:
            break;
    }
}

Upvotes: 1

Gricha
Gricha

Reputation: 1032

Ok so I created in my UIViewController a property segmentedControl and then wrote only that code:

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSArray *itemsArray = [[NSArray alloc] initWithObjects:@"a", @"b", nil];

    self.segmentedControl = [[UISegmentedControl alloc] initWithItems:itemsArray];
    self.segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
    self.segmentedControl.selectedSegmentIndex = 0;
    self.segmentedControl.frame = CGRectMake(0.0f, 0.0f, 320.0f,40.0f);
    [self.segmentedControl addTarget:self action:@selector(segmentedControlIndexChanged) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:self.segmentedControl];
}    

-(void)segmentedControlIndexChanged
{
    switch (self.segmentedControl.selectedSegmentIndex)
    {
        case 0:
            NSLog(@"createviews");
            break;
        case 1:
            NSLog(@"showfeedbackform");
            break;
        default:
            break;
    }
}

It works for me perfectly - logging the proper choices.

As you say the _segmentedControl is all right - notice that I use dot notation to get segmentedControl. This might be the case since when you're not using dot notation - you're not calling the getter.

Edit

To sum up a little - segmentedControl from your .h file is redundant - it's just another UISegmentedControl. Your property can be referenced by dot notation - self.segmentedControl. Or by synthesized name (made by default) _segmentedControl.

So what you're doing is referencing to this .h object when initializing and adding your UISegmentedController but when segmentedControlIndexChanged is called - you change it in your property which is not even visible.

Upvotes: 3

Related Questions