2012-04-18 139 views
1

所以這就是我所擁有的。我創建了一個按鈕並將其設置爲一個動作,但每次單擊該按鈕時它都會使程序崩潰。爲什麼我的按鈕不工作?提前致謝。編程添加按鈕不會工作

UIButton *currentGamesButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
currentGamesButton.frame = CGRectMake(124,18,72,65); 
[currentGamesButton addTarget:self 
         action:@selector(goToCurrentGamesViewController:) 
      forControlEvents:UIControlEventTouchDown]; 
UIImage *currentGamesPNG = [UIImage imageNamed:@"CurrentGamesHighlightedState.png"]; 
[currentGamesButton setBackgroundImage:currentGamesPNG forState:UIControlStateNormal]; 
[self.view addSubview:currentGamesButton]; 
+0

我懷疑它沒有找到選擇器goToCurrentGamesViewController:你能顯示代碼這個方法,包括它的聲明? – 2012-04-18 19:01:41

+0

在.h中:(IBAction)goToCurrentGamesViewController; – nfoggia 2012-04-18 19:13:04

+0

.m(IBAction)goToCurrentGamesViewController { – nfoggia 2012-04-18 19:13:26

回答

4

如果goToCurrentGamesViewController方法沒有參數,改變這一行:

[currentGamesButton addTarget:self 
        action:@selector(goToCurrentGamesViewController:) 

到:

[currentGamesButton addTarget:self 
        action:@selector(goToCurrentGamesViewController) 

(從該方法中刪除結腸:在選擇器)

+0

謝謝。解決了。您可以將什麼樣的參數添加到操作中?只是好奇。 – nfoggia 2012-04-18 19:17:16

+0

傳遞給選擇器中某個方法的參數只能包含一個id,這就是當':'進場時 – 2012-04-18 19:18:41