简体   繁体   中英

UIButton created in loop not clickable

Here's my problem:

I add several buttons to a scrollview in a loop:

for (int i = 0; i < [allEventArray count]; i++) {

        UIButton *eventButton = [UIButton buttonWithType:UIButtonTypeCustom];
        eventButton.tag =  i + 10;

        UIImage *normalImage = [UIImage imageNamed:@"rosterButton.png"];
        normalImage = [normalImage 
                      stretchableImageWithLeftCapWidth:normalImage.size.width/2 
                      topCapHeight:normalImage.size.height/2];
        [eventButton setBackgroundImage:normalImage 
                      forState:UIControlStateNormal];

        [eventButton addTarget:self action:@selector(gotoThisAirport) 
                      forControlEvents:UIControlEventTouchUpInside];

        [drawRosterView addSubview:eventButton];
}

I set the frame size and other stuff later on by opening the tag property. All the buttons appear as advertised. The problem is that only the first button in the view responds to clicking. The others just sit there chilling, doing nothing. How can this be? It driving me absolutely crazy!

Please help!

It sounds like your UIButton isn't fully inside your UIView.

Try only adding the UIButton to UIView and set a background color for the UIView to something like [UIColor greenColor] . You should then see if the UIButton is partially outside of the UIView.

Also, if you add an NSLog("click") call in -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event; of the UIView, then you can click anywhere on screen and get feedback if the UIView detects the touch or not.

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