Reputation:
In the B Viewcontroller, I store data into NSUserDefaults and retrieve them in AViewController.
During the execution, it performs as it expected. However, once I close and re-run the app, I figure out that these information (dictionary) does not being stored.
Here is the BViewController where I store the data in NSUserDefaults
partial class BViewController : UIViewController
{
const string server = "server";
const string port = "port";
const string password = "password";
const string username = "username";
const string inboxuserid = "inboxuserid";
.............
.............
public override void ViewWillDisappear (bool animated)
{
var appDefaults = new NSDictionary (server, serverTF.Text, port, portTF.Text, password, passwordTF.Text,username,usernameTF.Text, inboxuserid,inboxuserTF.Text);
NSUserDefaults.StandardUserDefaults.RegisterDefaults (appDefaults);
NSUserDefaults.StandardUserDefaults.Synchronize ();
}
}
and retrieve them in the A ViewController as follows:
partial class AViewController : UIViewController
{
public override void ViewWillAppear(bool animated)
{
var username = NSUserDefaults.StandardUserDefaults.StringForKey ("username");
Console.WriteLine (username);
}
}
Upvotes: 0
Views: 1221
Reputation: 16813
Based on the following article: https://moovendan.wordpress.com/2012/12/26/monotouch-nsuserdefaults-store-values-in-settings/
I would propose the following code, I have tested with a small example and it worked.
BViewController, store the dictionary in the NSUserDefaults:
NSDictionary appDefaults= new NSDictionary(server, serverTF.Text, port, portTF.Text, password, passwordTF.Text, username, usernameTF.Text, inboxuserid, inboxuserTF.Text);
NSString key = new NSString ("dict");
NSUserDefaults.StandardUserDefaults.SetValueForKey (appDefaults, key);
AViewController, retrive the dictionary from NSUserDefaults:
NSString key = new NSString ("dict");
NSDictionary d2 = NSUserDefaults.StandardUserDefaults.DictionaryForKey (key);
Console.WriteLine (d2["username"]);
Upvotes: 2