Reputation: 1184
I have a UIPickerView
which displays the array of items. When i select an item, it can be passed to all my view controller. Now my problem is after moving to different view controllers
and returned back to the UIPIckerView
it shows the first array item and not the item which i selected. How can i view the selected item?
//in viewDidLoad()
itemsArray = [[NSArray alloc] initWithObjects:@"5", @"10", @"20", @"30", @"50",@"100", nil];
// if i select 20 and moved to other pages of my controllers and
// return to the UIPickerView i can see the 5 as selected not the 20
Any suggestions?
Upvotes: 1
Views: 261
Reputation: 161
Hi use NSUserDefaults for holding the current selected value using ,
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[NSNumber numberWithInt:albumCount] forKey:@"AlbumCount"];
[defaults synchronize];
for retrieving sake ,
albumCount = [defaults integerForKey:@"AlbumCount"];
Upvotes: 2
Reputation: 20021
Can do it in different ways
Like
Upvotes: 1
Reputation: 11754
Depending on what you want to achieve, and how you're approaching opening a further view it may be as simple as declaring @property int currentIndex
, setting currentIndex = indexForSelectedItem
and then in your viewWillAppear
using it to alert your view what the last selected was.
Upvotes: 1
Reputation: 4791
You will need to store the selected value somewhere, either as a property on a view controller or model, or in a central location like NSUserDefaults
.
Then, when you return to that view controller with the UIPickerView
you can use
- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated
to set the value.
Upvotes: 1