[MonoTouch] NSUserDefaults

Kevin Daly kevindaly at xtra.co.nz
Thu Oct 8 12:36:50 EDT 2009


Sorry, I meant to post this reply to the list, forgot to hit ReplyAll:

 

Convert your string to an NSString before saving it, and use StringForKey
when retrieving it.

 

e.g. NSUserDefaults.StandardUserDefaults["LastName"] = new
NSString(textField.Text);

 

And to retrieve it:

 

If(row == 0)

{

                cell.TextLabel.Text = "Last Name";

                string name =
NSUserDefaults.StandardUserDefaults.StringForKey("LastName")??string.Empty
//1. Note StringForKey 2. The coalesce operator is your friend

                cell.ContentView.AddSubview(TextFieldNormal("your last
name", name, 1, UIKeyboardType.Default));

}

 

 

From: monotouch-bounces at lists.ximian.com
[mailto:monotouch-bounces at lists.ximian.com] On Behalf Of Manoj Nagpal
Sent: Friday, 9 October 2009 12:53 a.m.
To: monotouch at lists.ximian.com
Subject: [MonoTouch] NSUserDefaults

 

Hi All,

 

has anyone used NSUserDefaults to save application settings ? I have been
looking for code samples without any avail.

 

I want to save the string value for the Settings View. This doesn't seem to
be working. This is what I am trying:

 

To Save the value:

public override void EditingEnded (UITextField textField)

{

var prefs = NSUserDefaults.StandardUserDefaults;

              switch (textField.Tag)

              {

               case 0:

                        prefs["LastName"] = textField.Text;

                        break;

...

 

and to retrieve it:

public override UITableViewCell GetCell (UITableView tableView, NSIndexPath
indexPath)

{

...

            var prefs = NSUserDefaults.StandardUserDefaults;

...

            if (row == 0)

            {

                        cell.TextLabel.Text = "Last Name";

                        string obj = Convert.ToString(prefs["LastName"]);

                        if (obj == null)

                                    obj = "";

                        cell.ContentView.AddSubview (TextFieldNormal("your
last name", obj, 1, UIKeyboardType.Default));

            }

...

 

Please help.

 

Cheers

Manoj.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/monotouch/attachments/20091009/e94ca35c/attachment.html 


More information about the MonoTouch mailing list