【问题标题】:iPhone UIButton addTarget not workingiPhone UIButton addTarget 不起作用
【发布时间】:2010-11-13 04:16:12
【问题描述】:

在我的 iPhone 应用程序中,我有 SPGameDesk : UIView 类,它是从其他类创建的,例如:

SPGameDesk* gameDesk = [[SPGameDesk alloc] init];
[gameDesk createWithLevel:5];
[appWindow addSubview:gameDesk];

在我的SPGameDesk 类中,我用一种方法创建UIButtons

holesRow = [[NSMutableArray alloc] init];
for (int i=0; i<3; i++) {
    [holesRow addObject:[[UIButton alloc] initWithFrame:CGRectMake((40*i)+5, 150, 30, 30)]];
    [[holesRow objectAtIndex:i] setBackgroundColor:[UIColor blueColor]];
    [[holesRow objectAtIndex:i] addTarget:self action:@selector(holeSelected:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview: [holesRow objectAtIndex:i]]; 
}

在 SPGameDesk 类中,我有方法:

- (void)holeSelected:(id)sender {
    NSLog(@"SELECTED");
}

但是当我触摸我的按钮时,什么都没有发生,就像其他一些 UIView 覆盖了我的按钮(没有其他 UIView),或者像 SPGameDesk 禁用了 userInteraction(我检查了 - 它不是) .

我的问题在哪里?我应该检查什么?

【问题讨论】:

  • 您可以通过在之后循环并查看目标和操作指针是什么来调试它(确保它们不是零且正确。
  • 您确定holesRow 没有在某处发布吗?

标签: iphone objective-c cocoa-touch uibutton


【解决方案1】:

gameDesk 视图的框架是什么?看起来它可能是 0,0,0,0。尝试将其设置为父窗口的边界。使用 initwithframe 代替 init。

【讨论】:

  • 是的,你是对的!我只是没有考虑。感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-21
相关资源
最近更新 更多