【问题标题】:Programmatically added button wont work以编程方式添加的按钮不起作用
【发布时间】:2012-04-18 18:59:46
【问题描述】:

所以这就是我所拥有的。我创建了一个按钮并将其设置为一个动作,但每次我单击该按钮时都会使程序崩溃。为什么我的按钮不起作用?提前致谢。

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];

【问题讨论】:

  • 我怀疑它没有找到选择器 goToCurrentGamesViewController: 你能显示这个方法的代码,包括它的声明吗?
  • 在.h: (IBAction) goToCurrentGamesViewController;
  • 在.m (IBAction) goToCurrentGamesViewController{
  • 见下面我的回答。它适用于您的情况。

标签: ios uibutton programmatically-created


【解决方案1】:

如果goToCurrentGamesViewController 的方法不带参数,则更改此行:

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

到:

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

(从选择器中的方法中去掉冒号:

【讨论】:

  • 谢谢。解决了。您可以向操作添加哪些类型的参数?只是好奇。
  • 传递给选择器中方法的参数只能包含一个id,这就是:发挥作用的时候
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-20
  • 1970-01-01
  • 2015-04-01
  • 1970-01-01
相关资源
最近更新 更多