Say for example if I have two buttons one for apples and one for orange, and I select apples and takes me to the apples screen. How can I make it for now on every time I run the app it will go to the apples screen?
4 Answers
in viewDidLoad
if([[NSUserDefaults standardUserDefaults] objectForKey:@"fruit"] != nil) { if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"fruit"]isEqualToString:@"apple"]) { [self.navigationController pushViewController:appleVC animated:NO]; } else{ [self.navigationController pushViewController:orangeVC animated:NO]; } } and on Button Methods
on Apple button
[[NSUserDefaults standardUserDefaults] setObject:@"apple" forKey:@"fruit"]; on Orange button
[[NSUserDefaults standardUserDefaults] setObject:@"orange" forKey:@"fruit"]; Comments
You can store information like this using NSUserDefaults.
You'd store a boolean bAppleSelected like this:
NSUserDefaults * standardUserDefaults = [NSUserDefaults standardUserDefaults]; [standardUserDefaults setBool:bAppleSelected forKey=@"appleSelected"]; You can read it by accessing the default userDefaults:
BOOL bApple = [standardUserDefaults boolForKey=@"appleSelected"]; Comments
You can use NSUserdefaults here,
NSString* fruit=@"apple"; [[NSUserDefaults standardUserDefaults]setObject:fruit forKey:@"controllerName"]; [[NSUserDefaults standardUserDefaults]synchronize]; and insted of the name string of your firstview controller in appdelegate file use the above NSUserDefaults.