iphone - Unable to programmatically set UIButton action -
i'm having bit of trouble action on uibuttons. i'm creating 4 buttons programmatically i'm not able touchupinside event fire.
i've had read through i'm still having trouble pointers appreciated!
here's code create , set button , it's action:
uibutton *btn; float newwidth = 10; (int = 0; < _btnimages.count; ++i) { btn = [uibutton buttonwithtype:uibuttontyperoundedrect]; cgrect imageframe = cgrectmake(/*x*/newwidth, /*y*/height - 80, 65, 65); btn.frame = imageframe; btn.tag = i; [btn setbackgroundimage:[uiimage imagenamed:[_btnimages objectatindex:i]] forstate:uicontrolstatenormal]; [btn addtarget:self action:@selector(btnselected:) forcontrolevents:uicontroleventtouchupinside]; [btn setenabled:true]; [self addsubview:btn]; newwidth = newwidth + 75; } and here btnselected method:
-(void)btnselected:(uibutton *)button { nslog(@"button %ld clicked",(long int)[button tag]); }
the code have shown okay. possible problems are:
you adding buttons view has user interaction disabled (or of ancestor has user interaction disabled).
your buttons clipped superview. make sure buttons inside bounds of superview (and superview inside bounds of superview etc.).
other issues can happen when there gesture recognizers on views - can delay & cancel touches in subviews, including buttons. make sure touch event not handled gesture recognizer.
Comments
Post a Comment