ios - UIButton with target:self not being sent to self? -
i'm creating new uiviewcontroller (call myviewcontroller), , adding view (myview) subview of tableviewcell. within myview , there's button. button created programmatically during myviewcontroller's init, such:
-(id)initwithframe:(cgrect)frame { self = [super init]; if(self) { [self.view setframe:frame]; _yesbutton = [[uibutton alloc] initwithframe:cgrectmake(frame.size.width-150, 40, 140, 30)]; [_yesbutton settitle:@"yeah!" forstate:uicontrolstatenormal]; [_yesbutton addtarget:self action:@selector(didclick) forcontrolevents:uicontroleventtouchupinside]; [self.view addsubview:_yesbutton]; } return self; }
now, seems straightforward. displays properly, looks great in simulator.
but when click on "_yesbutton" within myview, crash error:
-[_uitableviewcellseparatorview didclick]: unrecognized selector sent instance 0x7b7f7ec0
what? when did "_uitableviewcellseparatorview" come equation? told _yesbutton set target "self", selector should sent myviewcontroller, right? imagine getting tripped , sending uitableviewcell, since myview embedded within tableviewcell, why seperatorview?
can tell me how _yesbutton send call myviewcontroller it's being created within? , bonus points, can explain how "_uitableviewcellseparatorview" became thing in conversation @ all?
edit: here's how i'm building cell in tableview, , adding myview it. note i'm deliberately not using dequeuing row, although might change if it's source of problem.
uitableviewcell *cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:@"myviewcell"]; myviewcontroller *myviewcontroller = [[myviewcontroller alloc] initwithframe:cell.contentview.frame]; [cell.contentview addsubview:myviewcontroller.view]; return cell;
and didclick method empty, (it never gets there, haven't gotten far in writing it), it's defined within myviewcontroller as:
-(void)didclick { }
solution #1
actually because myviewcontroller
not retain arc. dealloc
method id being called. add ans instance of controller in uitableviewcontroller
fix issue , make arc retains controller.
solution #2
try this.
create custom uitableviewcell
:
mycustomcell.h :
#import <uikit/uikit.h> @interface mycustomcell : uitableviewcell @property (strong, nonatomic) uibutton *yesbutton; -(id)initwithframe:(cgrect)frame; @end
mycustomcell.m :
#import "mycustomcell.h" @implementation mycustomcell - (void)setselected:(bool)selected animated:(bool)animated { [super setselected:selected animated:animated]; // configure view selected state } -(id)initwithframe:(cgrect)frame { self = [super init]; if(self) { [self.view setframe:frame]; _yesbutton = [[uibutton alloc] initwithframe:cgrectmake(frame)]; [_yesbutton settitle:@"yeah!" forstate:uicontrolstatenormal]; [self.contentview addsubview:_yesbutton]; } return self; } @end
in uitableviewcontroller
in viewdidload
function :
[self.tableview registerclass:[mycustomcell class] forcellreuseidentifier:cellidentifier];
then in tableview:cellforrowatindexpath:
mycustomcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifierr forindexpath:indexpath]; if (cell == nil) { cell = [[mycustomcell alloc] initwithframe:frame]; [cell.yesbutton addtarget:self action:@selector(didclick) forcontrolevents:uicontroleventtouchupinside]; }
and implement following in uitableviewcontroller
:
-(void)didclick { // following identify in cell action has been triggered nsset *touches = [event alltouches]; uitouch *touch = [touches anyobject]; cgpoint currenttouchposition = [touch locationinview:self.tableview]; nsindexpath *indexpath = [self.tableview indexpathforrowatpoint: currenttouchposition]; if (indexpath != nil) { // whatever want given cell } }
Comments
Post a Comment