I have a UISegmentedControl
in my ModeViewController
ModeViewController.h:
@property (weak, nonatomic) IBOutlet UISegmentedControl *Segment;
- (IBAction)switchMode:(id)sender;
ModeViewController.m:
- (IBAction)switchMode:(id)sender {
//I Tried this way
UISegmentedControl *segmentedControl = (UISegmentedControl *) sender;
_Segment.selectedSegmentIndex = segmentedControl.selectedSegmentIndex;
//or this way
NSInteger selectedSegment = segmentedControl.selectedSegmentIndex;
[_Segment setSelectedSegmentIndex:selectedSegment];
}
But as soon as I change to ViewController
and come back it will only display the default selected Segment! Any idea?
After you change view controller and you come back the viewDidLoad method called again thats why you are seeing the default selected index. The way to overcome this is store the selected the selected index into some app delegate property or you can create singleton class for the better design to preserve the state .And in your viewDidLoadMethod .
-viewDidLoad
{
AppDelegate *delegate=[[UIApplication sharedAppliection]delegate];
[_Segment setSelectedSegmentIndex:delegate.selectedIndex];
}
This wil work for you .