I am starting to learn how some iOS development at the minute and I currently have the Apress beginning IOS6 book that I am working off.
In chapter two there is a simple tutorial to show two buttons and a label and when a button is pressed it is shown on the label which one was pressed.
I have completed the tutorial but it has raised one question that I can't find the answer to.
The tutorial uses ARC (automatic reference counting) in case that makes a difference.
Here is the code,
The header file:
#import <UIKit/UIKit.h>
@interface MTMViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *statusLabel;
- (IBAction)buttonPressed:(UIButton *)sender;
@end
And the m file:
#import "MTMViewController.h"
@implementation MTMViewController
- (IBAction)buttonPressed:(UIButton *)sender {
NSString *title = [sender titleForState:UIControlStateNormal];
NSString *plainText = [NSString stringWithFormat:@"%@ button pressed.", title];
statusLabel.text = plainText;
}
@end
The above is how it appears in the book, however when doing the tutorial in Xcode I could not compile with the following line:
statusLabel.text = plainText;
And instead had to change it to:
_statusLabel.text = plainText;
When I done this the code compiled and ran fine, I tried to figure out why this happened by going back through the tutorial to see if I missed anything but I didn't see anything.
Can anyone explain why the code in the book didn't compile and why I had to add the underscore to the front of the variable? Is this correct or have I done something wrong?