Retired Document
Important: This version of Start Developing iOS Apps Today has been retired. The replacement version provides a new, more streamlined walkthrough of the basics. For information covering the same subject area as this page, please see "Tutorial: Basics".
Troubleshooting and Reviewing the Code
If you are having trouble getting your app to work correctly, try the problem-solving approaches described in this chapter. If your app still isn’t working as it should, compare your code with the listings shown at the end of this chapter.
Code and Compiler Warnings
Your code should compile without any warnings. If you do receive warnings, it’s recommended that you treat them as very likely to be errors. Because Objective-C is a very flexible language, sometimes the most you get from the compiler is a warning.
Check the Storyboard File
As a developer, if things don’t work correctly, your natural instinct is probably to check your source code for bugs. But when you use Cocoa Touch, another dimension is added. Much of your app’s configuration may be “encoded” in the storyboard. For example, if you haven’t made the correct connections, your app won’t behave as you expect.
If the text doesn’t update when you click the button, it might be that you didn’t connect the button’s action to the view controller, or that you didn’t connect the view controller’s outlets to the text field or label.
If the keyboard does not disappear when you click Done, you might not have connected the text field’s delegate or connected the view controller’s
textField
outlet to the text field. Be sure to check the text field’s connections on the storyboard: Control-click the text field to reveal the translucent connections panel. You should see filled-in circles next to thedelegate
outlet and thetextField
referencing outlet.If you have connected the delegate, there might be a more subtle problem (see the next section, “Delegate Method Names”).
Delegate Method Names
A common mistake with delegates is to misspell the delegate method name. Even if you’ve set the delegate object correctly, if the delegate doesn’t use the right name in its method implementation, the correct method won’t be invoked. It’s usually best to copy and paste delegate method declarations, such as textFieldShouldReturn:
, from the documentation.
Code Listings
This section provides listings for the interface and implementation files of the HelloWorldViewController
class. Note that the listings don’t show comments and other method implementations that are provided by the Xcode template.
The Interface file: HelloWorldViewController.h
#import <UIKit/UIKit.h> |
@interface HelloWorldViewController : UIViewController <UITextFieldDelegate> |
@property (copy, nonatomic) NSString *userName; |
@end |
The Implementation File: HelloWorldViewController.m
#import "HelloWorldViewController.h" |
@interface HelloWorldViewController () |
@property (weak, nonatomic) IBOutlet UITextField *textField; |
@property (weak, nonatomic) IBOutlet UILabel *label; |
- (IBAction)changeGreeting:(id)sender; |
@end |
@implementation HelloWorldViewController |
- (void)viewDidLoad |
{ |
[super viewDidLoad]; |
// Do any additional setup after loading the view, typically from a nib. |
} |
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation |
{ |
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); |
} |
- (IBAction)changeGreeting:(id)sender { |
self.userName = self.textField.text; |
NSString *nameString = self.userName; |
if ([nameString length] == 0) { |
nameString = @"World"; |
} |
NSString *greeting = [[NSString alloc] initWithFormat:@"Hello, %@!", nameString]; |
self.label.text = greeting; |
} |
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField { |
if (theTextField == self.textField) { |
[theTextField resignFirstResponder]; |
} |
return YES; |
} |
@end |
© 2013 Apple Inc. All Rights Reserved. (Last updated: 2013-04-23)