Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

Using plists!

Hey, im a newbie when it comes to xcode. So flame all you like for me asking this because we all had to start somewhere, so here we go. Is there anyone who would give a tut on using a plist to say... change the words on a lable when you press a button? I know you can do that by adding what text you'd liek the lable to say in the button method but i wanna learn how to use plists.
Post edited by Doodl3 on

Replies

  • Duncan CDuncan C Posts: 8,016Tutorial Authors, Registered Users
    Doodl3;311518 said:
    Hey, im a newbie when it comes to xcode. So flame all you like for me asking this because we all had to start somewhere, so here we go. Is there anyone who would give a tut on using a plist to say... change the words on a lable when you press a button? I know you can do that by adding what text you'd liek the lable to say in the button method but i wanna learn how to use plists.
    The simplest way to use plists is to create them with one of the writeToFile:atomically: methods.

    Say I want to create a plist with an array of strings:



    self.anArray = [NSArray arrayWithObjects: @\"one\", @\"two\", @\"three\"];
    NSString* documentsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]
    NSString* filePath = [documentsDir stringByAppendingPathComponent: @\"foo.plist\"];
    [anArray writeToFile: filePath atomically: TRUE];


    Then, reading the array from the plist could not be easier:

    NSString* documentsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]
    NSString* filePath = [documentsDir stringByAppendingPathComponent: @\"foo.plist\"];
    self.anArray = [NSArray arrayWithContentsOfFile: filePath];


    Once you've read the array from the plist, it's a regular array. You could use it to change the text on a button with code like this:

    -(IBAction) changeButtonText: (id) sender;
    {
    int index = arc4random % [self.anArray count];
    NSString* newText = [self.anArray objectAtindex: index];
    ((UIButton*) sender).text = newText;
    }


    There are probably a couple of minor syntax errors in the code above. I didn't proof it, much less compile and test it. It shows the basic idea.

    The code above assumes that it's being used from a single class that has a retained property anArray. You'll need to add code to your object's dealloc method to release the retained property anArray.
    Regards,

    Duncan C
    WareTo

    mug

    Animated GIF created with Face Dancer, available for free in the app store.
  • Doodl3Doodl3 Posts: 7New Users
    Duncan C;311524 said:
    The simplest way to use plists is to create them with one of the writeToFile:atomically: methods.

    Say I want to create a plist with an array of strings:



    self.anArray = [NSArray arrayWithObjects: @\"one\", @\"two\", @\"three\"];
    NSString* documentsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]
    NSString* filePath = [documentsDir stringByAppendingPathComponent: @\"foo.plist\"];
    [anArray writeToFile: filePath atomically: TRUE];


    Then, reading the array from the plist could not be easier:

    NSString* documentsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]
    NSString* filePath = [documentsDir stringByAppendingPathComponent: @\"foo.plist\"];
    self.anArray = [NSArray arrayWithContentsOfFile: filePath];


    Once you've read the array from the plist, it's a regular array. You could use it to change the text on a button with code like this:

    -(IBAction) changeButtonText: (id) sender;
    {
    int index = arc4random % [self.anArray count];
    NSString* newText = [self.anArray objectAtindex: index];
    ((UIButton*) sender).text = newText;
    }


    There are probably a couple of minor syntax errors in the code above. I didn't proof it, much less compile and test it. It shows the basic idea.

    The code above assumes that it's being used from a single class that has a retained property anArray. You'll need to add code to your object's dealloc method to release the retained property anArray.

    Thanks so much, ive read like 50 of your posts now and have learned more in a few hours than in a whole month of programming. All your posts are really great >.< I feel obliged to buy some of your apps. but now, lets see if i can make sense of this and get this to work. Thanks again!
Sign In or Register to comment.