objective c - Using addSubView or presentModalViewController in a standard method with paramaters? -
i'm trying reuse code, have tutorial view controllers / views, call action sheet. however, calling views different. tutorial view(s) need added subview , added navigation controller.
how can expand standard function cater these 2 different situations ?
you can see i'm having instead, means duplicate code :(
i have class called holds standard code, want add calls views here directly.
-(void)showhelpclickbuttonatindex:(int)buttonindex:(uiview *)vw { if (buttonindex == commonuihelppagesbtnidx) { // nothing } else if (buttonindex == 0) { nslog(@"tutorial here"); } }
i use in 1 view ...
- (void)actionsheet:(uiactionsheet *)actionsheet clickedbuttonatindex:(nsinteger)buttonindex { commonui *cui = [commonui alloc]; [cui showhelpclickbuttonatindex:buttonindex:self.view]; [cui release]; if (buttonindex == commonuihelppagesbtnidx) { uiviewcontroller *thecontroller = [[helpviewcontroller alloc] initwithnibname:@"helpview" bundle:nil onpage:helppagecalcbalance]; [self.navigationcontroller.topviewcontroller presentmodalviewcontroller:thecontroller animated:yes]; [thecontroller release]; } }
and view this...
- (void)actionsheet:(uiactionsheet *)actionsheet clickedbuttonatindex:(nsinteger)buttonindex { [cui showhelpclickbuttonatindex:buttonindex:self.view]; if (buttonindex == commonuihelppagesbtnidx) { thecontroller = [[helpviewcontroller alloc] initwithnibname:@"helpview" bundle:nil onpage:helppagegettingstarted]; [self.view addsubview:thecontroller.view]; } }
maybe actionsheets share 1 same delegate root viewcontroller or appdelegate know according it's current state. hence same actionsheet method used in both case.
if put appdelegate can reached actionsheetdelegate, should able gain control on every way present view, either modally or not in of views + window.
edit (re-edited code): maybe try this
myappdelegate.h:
@interface myappappdelegate : nsobject <uiapplicationdelegate, uiactionsheetdelegate> (...) - (void) showhelp;
myappdelegate.m:
- (void) showhelp { uiactionsheet *actionsheet = [[uiactionsheet alloc] initwithtitle:@"" delegate:self cancelbuttontitle:@"cancel" destructivebuttontitle:nil otherbuttontitles: @"tutorial", @"help pages", nil]; actionsheet.actionsheetstyle = uiactionsheetstyleblackopaque; [actionsheet showinview:window]; [actionsheet release]; } - (void)actionsheet:(uiactionsheet *)actionsheet clickedbuttonatindex:(nsinteger)buttonindex { if (buttonindex == commonuihelppagesbtnidx) { uiviewcontroller *thecontroller = [helpviewcontroller alloc]; if ([[self lvc] superview] != nil) { thecontroller = [initwithnibname:@"helpview" bundle:nil onpage:helppagegettingstarted]; [window addsubview:thecontroller.view]; } else { thecontroller = [initwithnibname:@"helpview" bundle:nil onpage:helppagecalcbalance]; uinavigationcontroller * navcontroller = [tabbarcontroller selectedviewcontroller]; [navcontroller.topviewcontroller presentmodalviewcontroller:thecontroller animated:yes]; } [thecontroller release]; } }
and in viewcontrollers:
- (void)viewdidload { [super viewdidload]; myappdelegate *delegate = (myappdelegate *) [[uiapplication sharedapplication] delegate]; uibutton *infobutton = [uibutton buttonwithtype:uibuttontypeinfolight]; [infobutton addtarget:delegate action:@selector(showhelp) forcontrolevents:uicontroleventtouchupinside]; uibarbuttonitem *infoitem = [[uibarbuttonitem alloc] initwithcustomview:infobutton]; self.navigationitem.rightbarbuttonitem = infoitem; [infoitem release]; }
didn't try should work.
Comments
Post a Comment