简体   繁体   English

iPhone UIButton addTarget不起作用

[英]iPhone UIButton addTarget not working

In my iPhone app, I have SPGameDesk : UIView class, creating it from other class like: 在我的iPhone应用程序中,我有SPGameDesk : UIView类,它是从其他类创建的,例如:

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

In my SPGameDesk class, in one method, I create UIButtons : 在我的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]]; 
}

And lower in SPGameDesk class I have method: 在SPGameDesk类的下面,我有方法:

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

But when I touch my button, nothing happens, like some other UIView covers my button (there is no other UIView ), or like SPGameDesk has userInteraction disabled (I checked - it is not). 但是当我触摸我的按钮时,什么也没有发生,就像其他UIView覆盖了我的按钮(没有其他UIView )一样,或者SPGameDesk禁用了userInteraction (我检查了-否)。

Where is my problem? 我的问题在哪里? What should I check? 我应该检查什么?

What is the frame for your gameDesk view? 您的gameDesk视图的框架是什么? It looks like it could be 0,0,0,0. 看起来可能是0,0,0,0。 Try setting it to the bounds of the parent window. 尝试将其设置为父窗口的边界。 Use initwithframe instead of init. 使用initwithframe代替init。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM