I used NSUserDefaults to store the value. I store some integer value in NSUserDefaults, as shown below in the first view.
I stated in the interface file as follows.
NSUserDefaults *prefs;
@property(nonatomic,retain) IBOutlet NSUserDefaults *prefs;
Then I used in the class file as follows
@synthesize passwordfield,prefs;
here passwordfield is the name of UITextField
NSInteger myInteger = [passwordfield.text integerValue];
[prefs setInteger:myInteger forKey:@"integerKey"];
[prefs synchronize];
NSInteger myInt = [prefs integerForKey:@"integerKey"];
NSLog(@"My integer Value: %d",myInt);
Then I move on to the next view using the navigation controller.
But I can not read the value of NSUserDefaults from the first view.
I used the following code in the second view.
PasswordCheckViewController *nextController = [[PasswordCheckViewController alloc] initWithNibName:@"PasswordCheckViewController" bundle:nil];
NSLog(@"Pass in Next: %d",[nextController.prefs integerForKey:@"integerKey"]);
But the conclusion is as follows.
Pass in Next: 0
How can I get the value from the first view for the second view using NSUserDefaults?
source
share