I have implemented several of UISegmentedControl objects before but for one or other reason I am unable to implement it within a tableview cell. I want the user to choose his gender. if the value of the segmentedControl changed a method gets called to save it for further processing.
self.seg = [[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:@"Man",@"Female", nil]];
[self.seg addTarget:self action:@selector(toggleGender) forControlEvents:UIControlEventValueChanged];
I created the method:
-(void)toggleGender{
NSLog(@"%d",self.seg.selectedSegmentIndex)
if (self.seg.selectedSegmentIndex == 0)
{
NSLog(@"User is a man");
} else if (self.seg.selectedSegmentIndex == 1)
{ NSLog(@"User is a female");
}
}
The first NSLog prints -1 as value if the value has changed.
I declared seg in the header file, and created a property and synthesized it!
I am currently NOT dealllocing this object. (I know this is a memory leak)
Why am I unable to read-out the selected segment, and why does it give back -1 as selected item?
Try this :
(NOTE THE ‘:’ in the @selector..). And change your function by this :