Adding and changing labels on the iPhone? (IOS4+)
I'm trying to write a "hello world" for iPhone 4. As this is my first attempt at iPhone app development, and my first experience with Objective C, please feel free to assume you need to teach me how to suck eggs on this one.
As my foundation, I started with the http://appsamuck.com/day1.html tutorial - but it's so far out of date I'm not going to get anywhere unless I de-construct the steps.
At this point I'm looking at the part where he tells you how to add a label to the display AND add the reference to the label.
Unfortunately it just doesn't seem to be fitting together properly.
Essentially, what I'd like the simulator to do when I run the "Built and Run" button is for the "MyLabel" to change to read "HERE"
When this all runs, there are no errors, but cdLabel.text is not changing to read "HERE".
I've got a photo of the basic GUI setup - and can post any other information needed.
the ViewController.h
reads:
@interface MinutesToMidnightViewCo开发者_运维技巧ntroller : UIViewController {
IBOutlet UILabel *cdLabel;
}
-(void)updateLabel;
the ViewController.m
has the following function in it:
-(void)updateLabel {
cdLabel.text = [NSString: "HERE"];
}
Finally - there's a photo of how I've set up the GUI bits in the interface designer:
If there's anyone who can connect the dot on this last bit I'd be so very grateful! (Also directions to IOS4 beginner's tutorials or open source APPS would be highly valued :)
Thanks! - A
UPDATE:
In response to the question of when do I call updateLabel
I have the function
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[viewController updateLabel];
}
built into my MinutesToMidnightAppDelegate.m
file
Synthesize cdLabel in your .m, and set your label.text in viewDidLoad. Also, [NSString: "Here"] will not work. Just use cdLabel.text = @"HERE";
(Which is the same as: cdLabel.text = [NSString stringWithFormat:@"HERE"];
Well written question (+1 for that)
I'm surprised this line compiles:
cdLabel.text = [NSString: "HERE"];
try this:
cdLabel.text = @"HERE"; // this is how you write a literal NSString
if that doesn't work, put a breakpoint on this line and check that cdLabel is not nil. If it is nil, then go back and double check your connection in IB. It's hard to see that the resolution you posted, but it looks OK if I squint at it. :)
Also, where is updateLabel called?
EDIT
applicationDidFinishLaunching: is called before your viewController's view is normally loaded. In fact, that method will usually present that first viewController's view.
In any case, you should set up your view in -[UIViewController viewDidLoad], which is called after that view is loaded (as you would probably guess from the name)
or -[UIViewController viewWillAppear] if you want to do this each time the view appears.
精彩评论