xcode - callback with UIButton Event -
i have error says "exec_bad_access". want run callback after uibutton clicked.
here code:
dialog.m
- (void)showdialoginparentview: (uiview *)parentview withtext:(nsstring *)text leftbtntext:(nsstring *)t1 rightbtntext:(nsstring *)t2 handler:(void(^)(int))handler { vhandler = [handler copy]; // init control button selector(event) // _rightbtn = [uibutton buttonwithtype:uibuttontypecustom]; _rightbtn.frame = cgrectmake((self.altview.frame.size.width / 2), 1.0, (self.altview.frame.size.width / 2), self.altview.frame.size.height); _rightbtn.titlelabel.textcolor = [uicolor whitecolor]; [_rightbtn settitle:@"bla bla" forstate:uicontrolstatenormal]; [_rightbtn addtarget:self action:@selector(button_click:) forcontrolevents:uicontroleventtouchupinside]; // somethings here // } - (void)button_click: (uibutton *)sender { // callback here when click button // vhandler(1); } here code calls:
foo.m
-(void)showdialog { dialog *d = [[dialog alloc] init]; [d showdialoginparentview:self.view withtext:@"bla" leftbtntext:@"bla bla" rightbtntext:@"bla bla bla" handler:^(int res) { // somethings // }]; } code works fine when run vhandler in *- (void)showdialoginparentview: (uiview *)parentview withtext:(nsstring *)text leftbtntext:(nsstring *)t1 rightbtntext:(nsstring )t2 handler:(void(^)(int))handle method. want run vhandler when uibutton clicked.
i've solved problem myself. problem 'vhandler' null after init dialog class. used static variable has solved.
dialog.h
+ (void)showdialoginparentview: (uiview *)parentview withtext:(nsstring *)text leftbtntext:(nsstring *)t1 rightbtntext:(nsstring *)t2 handler:(void(^)(int))handler; foo.m
static vhandler _handler;
Comments
Post a Comment