【问题标题】:UIControl - addTarget: action: forControlEvents doesn't call the method inside selectorUIControl - addTarget: action: forControlEvents 不调用选择器内的方法
【发布时间】:2012-05-20 19:39:51
【问题描述】:

我在向我的应用程序添加目标/操作方法时遇到问题。这是我的代码:

-(void)printInConsole
{
  NSLog(@"2");
}

-(void)createGCButton
{
  UIButton * LButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  LButton.frame = CGRectMake(200, 90, 100, 50);
  [LButton setTitle:@"L" forState:UIControlStateNormal];
  [self.view addSubview:LButton];
  [LButton addTarget:self action:@selector(printInConsole)forControlEvents:UIControlEventTouchUpInside];
}

所以我首先尝试了一种游戏中心方法,但没有成功。现在我添加了这个简单的 NSLog 方法,它也不起作用。控制台什么也不写。那么问题出在哪里。如果您需要有关我的代码的更多详细信息,请说出来,我会发布它。上帝保佑解决这个问题的人。

问候,克莱门

【问题讨论】:

  • 当你按下按钮时,它看起来像被按下了吗(即它会改变颜色)吗?
  • 将此代码粘贴到一个干净的项目中是可行的,因此问题不在您发布的代码中。您是否偶然在视图中使用了 UITapRecognizers?你在使用 ARC 吗?
  • @Joel 按钮改变颜色
  • @PaulHunter 我同时使用 UITapRecognizers 和 ARC
  • 这段代码运行良好。清理您的项目并重试。你用的是哪个xcode?​​span>

标签: iphone uicontrol target-action


【解决方案1】:

问题很可能与UITapGestureRecognizer 有关。你可以看到这个blog post我提出的这个问题。

基本上你需要实现一个UITapGestureRecognizer 委托方法并告诉它不要乱用按钮。

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    return ![touch.view isKindOfClass:[UIButton class]];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-07
    相关资源
    最近更新 更多