iPhone的UIButton addTarget不工作
问题描述:
在我的iPhone应用程序,我有SPGameDesk : UIView
类,其他类,如创建它:iPhone的UIButton addTarget不工作
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
(我检查 - 它不是)。
我的问题在哪里?我应该检查什么?
答
什么是您的gameDesk视图的框架?它看起来可能是0,0,0,0。尝试将其设置为父窗口的界限。使用initwithframe而不是init。
+0
是的你是对的!我只是没有想到它,Thx为你的帮助! – splatt 2010-11-13 13:59:08
你可以通过循环查看目标和动作指针(确保它们不是零和正确)来调试它。 – 2010-11-13 04:57:09
你确定holesRow没有被释放吗? – raidfive 2010-11-13 05:01:17