简体   繁体   中英

iPhone UIButton addTarget not working

In my iPhone app, I have SPGameDesk : UIView class, creating it from other class like:

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

In my SPGameDesk class, in one method, I create 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:

- (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).

Where is my problem? What should I check?

What is the frame for your gameDesk view? It looks like it could be 0,0,0,0. Try setting it to the bounds of the parent window. Use initwithframe instead of init.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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