I have two viewcontrollers. In my first viewcontroller:
{
NSString *string=textField.text;
NSUserDefaults *data = [NSUserDefaults standardUserDefaults];
[data setObject:string forKey:@"strings"];
[data synchronize];
}
How do I get the string value in my other viewcontroller?
you can access
NSUserDefaults
in any controller (Any class of your application) of your application with the same code you have written in one class.for getting the string value use the below code
In the other ViewController,
This will work because
NSUserDefaults
are a global key-value store that will persist across classes and applications.THis is the way to retrieve the data. Please note
NSUserDefault
is not used to pass data between two controllers. There are better methods for that.Edit : After seeing Shaan Singh's comment
To pass data 2 view controllers you can declare a property in second view controller and access that from the present view controller.
It is already answered brilliantly here.
Here you can use this in anyway in your application for store value of NSUserDefaults.
Here you can use this in anyway in your application for get value of NSUserDefaults.