- In SJSUViewController.h, add
@property (nonatomic, retain) NSArray *choices;
- In SJSUViewController.m, add
@synthesize choices;
after @implementation
- In
viewDidLoad, add the following code after [super viewDidLoad];:
self.choices = [NSArray arrayWithObjects: @"Android", @"Blackberry", @"iOS", @"Windows Mobile"];
- You get a warning triangle. What's the message?
- Apply the suggested fix. What happens?
- In
viewDidReceiveMemoryWarning, add
self.choices = nil;
This helps with the semi-automatic memory management.
- In SJSUViewController.h, add the following under the array:
@property (nonatomic, retain) IBOutlet UILabel *choice_text;
-(IBAction)choice_btn_touch:(id)sender;
choice_text is a property, so what do you need to do in ViewController.m?
- And in
ViewDidReceiveMemoryWarning?
- Now we add the implementation of the button callback. Add it before
@end in the .m file:
-(IBAction)choice_btn_touch:(id)sender {
int count = [self.choices count];
int index = arc4random() % count;
NSString *choice = [self.choices objectAtIndex:index];
self.choice_text.text = choice;
}
- Now select the .xib file. Keep the label from the previous step and add a Round Rect Button.
- Select View → Assistant Editor
- Right-click the label and drag to the
choice_text property.
- Right-click the button. Drag to the callback function.
- Run your program. What does it do when you click the button?