uibutton - Programmatically created a button on iOS, but no action when pressed -
i created button code below, not respond in log (supposed show "ha"). strangest part have been able exact code work inside collection view, not on viewcontroller. doing wrong button press "myaction"?
-(void)viewdidload { uibutton *buttonz = [uibutton buttonwithtype:uibuttontypecustom]; buttonz.frame = cgrectmake(160, 0, 160, 30); [buttonz settitle:@"charge" forstate:uicontrolstatenormal]; [buttonz addtarget:self action:@selector(myaction:) forcontrolevents:uicontroleventtouchupinside]; [buttonz setenabled:yes]; [buttonz setbackgroundcolor:[uicolor bluecolor]]; //add button view [backimageview addsubview:buttonz]; } -(void)myaction:(id)sender { nslog(@"ha"); [self resignfirstresponder]; nslog(@"ha"); }
1)
you don't need "resignfirstresponder
" on button (seems of time see "resignfirstresponder
" in text fields or text views).
2)
change control event uicontroleventtouchupinside uicontroleventtouchdown
3)
the parent uiimageview might not have "userinteractionenabled
" set true, means events won't propogated (or sent along) subviews.
to make events available subviews, change parent view's userinteractionenabled property via:
backimageview.userinteractionenabled = yes;
before adding subview.
Comments
Post a Comment