First iOS App_Troubleshooting and Reviewing the Code
错误检查和代码重查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
你的代码编译后应该没有警告。如果还是有警告,它的建议是你处理它们像处理错误一样。因为OC是一个灵活的语言,有时编译器通常会提示警告。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
作为一个开发者,如果不能运行,本能是可能查看你的源代码找错误。但是当你用 Cocoa Touch时,会添加另一个尺寸。你的大部分应用配置可能是用 storyboard 拖拽实现的。例如,如果你没有正确连接,你的应用不会如期运行。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.
-
如果点击完成时键盘没有解除,可能是没有连接文本区代理,或者没有连接视图控制器的文本区属性连线到文本区。要连接文本区到 stroyboard:控制-点击文本区,解开半透明的连接面板。你应该使代理连线和文本区映射连线的环形图标变成实心。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
textFieldoutlet 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 thedelegateoutlet and thetextFieldreferencing outlet.如果你已经连接了代理,可能会有一个更微妙的问题(查看下一章:“代理方法名称”)。If you have connected the delegate, there might be a more subtle problem (see the next section, “Delegate Method Names”).
代理方法名字Delegate Method Names
一个常见的关于代理的错误是拼错了代理方法名。如果你已经正确设置代理对象,如果代理没有在实现该方法使用正确的名字,正确的方法是不会被调用的。最好是复制并粘贴代理方法的声明到头文件,例如从文件复制并粘贴 textFieldShouldReturn: 方法。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
这一节提供HelloWorldViewController类的接口及实现文档的清单。请注意,列表不显示注释和 Xcode 的模板所提供的其他方法实现。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.
头文件:HelloWorldViewController.hThe Interface file: HelloWorldViewController.h
#import <UIKit/UIKit.h> |
@interface HelloWorldViewController : UIViewController <UITextFieldDelegate> |
@property (copy, nonatomic) NSString *userName; |
@end |
实现文件:HelloWorldViewController.mThe 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 |

浙公网安备 33010602011771号