2012-04-18 126 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