2010-09-29 104 views
0

如何调试iPhone应用程序?我怎样才能知道模拟器中发生了什么?我是Xcode开发人员的初学者,不知道下面的代码有什么问题。该应用程序在点击按钮时崩溃。调试iphone应用程序

- (void)viewDidLoad { 

    myLabel = [[UILabel alloc]init]; 

    [myLabel setText:@"Labela"]; 
    myLabel.frame = CGRectMake(50.0,50.0, 100.0, 30.0); 
    [self.view addSubview:myLabel]; 

    myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 

    [myButton addTarget:self 
       action:@selector(buttonAction:) 
     forControlEvents:UIControlEventTouchDown]; 

    [myButton setTitle:@"Klikni" forState:UIControlStateNormal]; 
    [myButton setFrame:CGRectMake(80.0, 210.0, 160.0, 40.0)]; 

    [self.view addSubview:myButton]; 
    [super viewDidLoad]; 

} 

- (void)buttonAction { 
    [myLabel setText:@"Promijenjen!"]; 
} 

回答

3
action:@selector(buttonAction:) 

在这里您可以指定buttonAction选择得1个参数,但它被宣布为具有无:

- (void)buttonAction{ 
... 

所以,当按钮点击系统试图调用未定义的方法和结果在崩溃。为了解决这个问题,你应该要么改变选择的名字

action:@selector(buttonAction) 

或变化的行动方法声明:

- (void)buttonAction:(id)sender{ 
    ... 
1

Alt +点击“Build & Run”按钮进行调试。点击“显示控制台”按钮。使用NSLog和断点。 尝试声明: - (void)buttonAction:(id)sender;

1

如果你是初学者,你应该从一个教程或更好的开始,good book关于这个问题。

您可以输出一个消息,使用NSLog(@"My variable value: %@", myVariable);

您可以使用调试,一行行,只是在代码和运行方式调试添加断点任何地方的控制台。