IPhone : Creating A Preference To Reopen Last Selected Item In UITable

on Saturday, March 28, 2015

I would like to allow my application to reopen the last item you select from a UITable. I have to store my preferences inside of my didSelectRowAtIndexPath method like so:



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:self.carData[indexPath.section][indexPath.row] forKey:kLastSelectedItem];
[userDefaults synchronize];

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
// NSDate *object = _objects[indexPath.row];
self.detailViewController.detailItem=
self.carData[indexPath.section][indexPath.row];
}
}


Then I need to update the setting in viewDidLoad.



- (void)viewDidLoad
{
[super viewDidLoad];

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

if([userDefaults objectForKey:kLastSelectedItem]) {
self.detailViewController.detailItem = [userDefaults objectForKey:kLastSelectedItem];
}

self.detailViewController =
(DetailViewController *)[[self.splitViewController.viewControllers
lastObject] topViewController];
[self createCarData];
}


I believe I am missing a call for prepareForSegue in the viewDidLoad method to make sure that kSelected constant contains a value before calling the prepareForSegue method. But the thing is I don't know how. How can I do this?


0 comments:

Post a Comment