Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am new to apps building with the Xcode 4 Storyboard. I now stuck in figuring out

1. How to store a list of texts - short phrases (data) then,

2. How to display these texts randomly in the storyboard View Controllers.

I am reading the Foundation of iPhone App Development by Nick Kun, just like what i google online, they are teaching on table view controller in how to store and retrieve from the core data, not very much close to my objective. Please advice, thanks in advance.

share|improve this question

1 Answer

up vote 0 down vote accepted
  • Create a plist file in Xcode, set it's root as an array and add your texts to it.

  • Read from the plist using this (this assumes you named your file randomstrings.plist):

.

NSString *path = [[NSBundle mainBundle] pathForResource:
                  @"randomstrings" ofType:@"plist"];
NSArray *randomStrings = [NSArray arrayWithContentsOfFile:path];
  • Create a IBOutlet UILabel property in the view controller header file:

.

@property (nonatomic, strong) IBOutlet UILabel *randomLabel;
  • Link up the outlet on your storyboard (control drag from "File Owner" to a UILabel that you've added to your view)

  • Get a random string by doing something like this:

.

int randomNumber = random() % ([randomString count] - 1);
NSString *string = [randomString objectAtIndex:randomNumber];
_randomLabel.text = string;
share|improve this answer
Hi, i somehow faced a little errors when going through it. Please correct me on my steps; (!) I create the plists and do as mentioned, and i added in 5 strings. (2) I added the following coding to the viewcontroller.m which my random text will be displaying (3) i created IBOutlet UILabel property in the specific viewcontroller.h by control-drag from storyboard (4) Lastly, i added the int randomNumner... coding in the viewcontorller.m the error occurs on the randomstrings not being used and unable to run. Please tell me which part I went wrong. Thankyou. – Jasper Goh Jun 1 at 3:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.