Friday, 15 August 2014

iphone - iOS - sharing code between view controllers -



iphone - iOS - sharing code between view controllers -

i have 2 view controllers - uiviewcontroller , uitableviewcontroller.

and both require same code barbuttonitems - done button , settings button. think it's ugly having same code method selectors , in viewdidload, can't think of other way have same barbuttonitems 2 controllers.

the shared code looks like:

- (void)dismissviewcontroller { [self.navigationcontroller dismissviewcontrolleranimated:yes completion:nil]; } - (void)done:(uibarbuttonitem *)sender { [self dismissviewcontroller]; } - (void)settings:(uibarbuttonitem *)sender { [self performseguewithidentifier:settings_segue sender:sender]; } - (void)viewdidload { [super viewdidload]; if (self.navigationcontroller.viewcontrollers.count == 1) self.navigationitem.leftbarbuttonitem = [[uibarbuttonitem alloc] initwithtitle:@"settings" style:uibarbuttonitemstylebordered target:self action:@selector(settings:)]; self.navigationitem.rightbarbuttonitem = [[uibarbuttonitem alloc] initwithbarbuttonsystemitem:uibarbuttonsystemitemdone target:self action:@selector(done:)]; }

architecturally, code this:

uitabbarcontroller -view controllers-> uinavigationcontroller -root view controller-> uiviewcontroller uinavigationcontroller -root view controller-> uitableviewcontroller

so, there way share code between uiviewcontroller , uitableviewcontroller? :)

if want, create static method initnavigationbar pass in navigationitem.

+ (void ) initnavigationbar: (uinavigationitem *) navigationitem { navigationitem.leftbarbuttonitem = [[uibarbuttonitem alloc] initwithtitle:@"settings" style:uibarbuttonitemstylebordered target:selfaction:@selector(settings:)]; navigationitem.rightbarbuttonitem = [[uibarbuttonitem alloc] }

but think twice if makes sense.

iphone refactoring uitabbarcontroller viewcontroller

No comments:

Post a Comment