Sabarish
Sabarish

Reputation: 1184

Showing UIPickerview selected value

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

Answers (4)

Mahesh Aswathanarayana
Mahesh Aswathanarayana

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

Lithu T.V
Lithu T.V

Reputation: 20021

Can do it in different ways

  1. Use pass by value or reference in different VCs
  2. Store the selected value in some other place which is persistant throughout the runtime and use it

Like

  • NSUserdefaults,
  • Inside a singleton instance[bad idea,memory retained all time but still an option]
  • SQLite DB [ok ok,still hard in this case alone]
  • coredata [ sofisticated in this purpose]
  • In a file [write and read from file operations]

Upvotes: 1

Nicholas Smith
Nicholas Smith

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

Jeff
Jeff

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

Related Questions