2010-09-29 175 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);

您可以使用調試,一行行,只是在代碼和運行方式調試添加斷點任何地方的控制檯。