ios - How to reuse this ViewController code in an object oriented way? -
i have app 4 viewcontrollers, , there lot of code repeating same way in 4 view controllers, , know best oop way write code 1 time , reuse in others view controllers.
this code viewdidload of first view controller:
- (void)viewdidload { [super viewdidload]; [self setcandisplaybannerads:yes]; [[[self.tabbarcontroller.viewcontrollers objectatindex:1] tabbaritem]setimage:[uiimage imagenamed:@"iconobota30x30.png"]]; [[[self.tabbarcontroller.viewcontrollers objectatindex:2] tabbaritem]setimage:[uiimage imagenamed:@"actividades.png"]]; [[[self.tabbarcontroller.viewcontrollers objectatindex:3] tabbaritem]setimage:[uiimage imagenamed:@"nosotros.png"]]; float width = [uiscreen mainscreen].bounds.size.width; float height = [uiscreen mainscreen].bounds.size.height; static_ivar = [[uiwebview alloc] initwithframe:cgrectmake(0, 0, width,height)]; static_ivar.delegate = self; nsurl *url = [nsurl urlwithstring:@"http://guiasdelsur.es"]; nsurlrequest *requestobj = [nsurlrequest requestwithurl:url]; [static_ivar loadrequest:requestobj]; [self.view addsubview:static_ivar]; //gestures //down uiswipegesturerecognizer *gest1 = [[uiswipegesturerecognizer alloc] initwithtarget:self action:@selector(hidetabbar:)]; gest1.delegate = self; [gest1 setdirection:uiswipegesturerecognizerdirectiondown]; [self.view addgesturerecognizer:gest1]; //up uiswipegesturerecognizer *gest2 = [[uiswipegesturerecognizer alloc] initwithtarget:self action:@selector(showtabbar:)]; gest2.delegate = self; [gest2 setdirection:uiswipegesturerecognizerdirectionup]; [self.view addgesturerecognizer:gest2]; //first tab _tabinicio = [[uitabbaritem alloc] initwithtitle:@"inicio" image:[uiimage imagenamed:@"d.png"] tag:0]; [[uitabbar appearance] setselectedimagetintcolor:[uicolor greencolor]]; [self.tabbarcontroller setselectedindex:0]; self.tabbaritem = _tabinicio; _ctoolbar = [[uitoolbar alloc] init]; _ctoolbar.frame = cgrectmake(0, 0, self.view.frame.size.width, 44); nsmutablearray *items = [[nsmutablearray alloc] init]; [_ctoolbar setbarstyle:uibarstyleblack]; [_ctoolbar settranslucent:yes ]; [_ctoolbar settintcolor:[uicolor greencolor]]; // additional setup after loading view, typically nib. nsstring *backarrowstring = @"\u000025c0\u0000fe0e atrĂ¡s"; //black left-pointing triangle plus variation selector uibarbuttonitem *back = [[uibarbuttonitem alloc] initwithtitle:backarrowstring style:uibarbuttonitemstyledone target:nil action:@selector(goback)]; uibarbuttonitem *right = [[uibarbuttonitem alloc] initwithbarbuttonsystemitem:uibarbuttonsystemitemrefresh target:nil action:@selector(refreshcontrol)]; [right settintcolor:[uicolor greencolor]]; uibarbuttonitem *flexiblespace = [[uibarbuttonitem alloc] initwithbarbuttonsystemitem:uibarbuttonsystemitemflexiblespace target:nil action:nil]; [items addobject:back]; [items addobject:flexiblespace]; [items addobject:right]; [_ctoolbar setitems:items animated:no]; [self.view addsubview:_ctoolbar]; //iad adbannerview * adview = [[adbannerview alloc] initwithframe:cgrectzero]; adview.frame = cgrectoffset(adview.frame, 0, 44); adview.delegate = self; [self.view addsubview:adview]; //loading indicator _indicator = [[uiactivityindicatorview alloc] initwithactivityindicatorstyle:uiactivityindicatorviewstylegray]; [_indicator setcenter:self.view.center]; [_indicator sethideswhenstopped:yes]; [self.view addsubview:_indicator]; [_indicator startanimating]; }
and code viewdidload
method of secondviewcontroller
, similar first one. differences in view controllers same ones.
- (void)viewdidload { [super viewdidload]; [self setcandisplaybannerads:yes]; float width = [uiscreen mainscreen].bounds.size.width; float height = [uiscreen mainscreen].bounds.size.height; static_ivar2 = [[uiwebview alloc] initwithframe:cgrectmake(0, 0, width,height)]; static_ivar2.delegate = self; nsurl *url = [nsurl urlwithstring:@"http://guiasdelsur.es/category/programas/"]; nsurlrequest *requestobj = [nsurlrequest requestwithurl:url]; [static_ivar2 loadrequest:requestobj]; [self.view addsubview:static_ivar2]; //second tab _secondtab = [[uitabbaritem alloc] initwithtitle:@"programas" image:[uiimage imagenamed:@"iconobota30x30.png"] tag:1]; [[uitabbar appearance] setselectedimagetintcolor:[uicolor greencolor]]; [self.tabbarcontroller setselectedindex:1]; self.tabbaritem = _secondtab; //gestures //down uiswipegesturerecognizer *gest1 = [[uiswipegesturerecognizer alloc] initwithtarget:self action:@selector(hidetabbar:)]; gest1.delegate = self; [gest1 setdirection:uiswipegesturerecognizerdirectiondown]; [self.view addgesturerecognizer:gest1]; //up uiswipegesturerecognizer *gest2 = [[uiswipegesturerecognizer alloc] initwithtarget:self action:@selector(showtabbar:)]; gest2.delegate = self; [gest2 setdirection:uiswipegesturerecognizerdirectionup]; [self.view addgesturerecognizer:gest2]; uitoolbar *ctoolbar = [[uitoolbar alloc] init]; ctoolbar.frame = cgrectmake(0, 0, self.view.frame.size.width, 44); nsmutablearray *items = [[nsmutablearray alloc] init]; [ctoolbar setbarstyle:uibarstyleblack]; [ctoolbar settranslucent:yes ]; [ctoolbar settintcolor:[uicolor greencolor]]; uibarbuttonitem *flexiblespace = [[uibarbuttonitem alloc]initwithbarbuttonsystemitem:uibarbuttonsystemitemflexiblespace target:nil action:nil ]; uibarbuttonitem *right = [[uibarbuttonitem alloc] initwithbarbuttonsystemitem:uibarbuttonsystemitemrefresh target:nil action:@selector(refreshcontrol)]; [right settintcolor:[uicolor greencolor]]; [items addobject:flexiblespace]; [items addobject:right]; [ctoolbar setitems:items animated:no]; [self.view addsubview:ctoolbar]; //iad adbannerview * adview = [[adbannerview alloc] initwithframe:cgrectzero]; adview.frame = cgrectoffset(adview.frame, 0, 44); adview.delegate = self; [self.view addsubview:adview]; //indicator _indicador = [[uiactivityindicatorview alloc] initwithactivityindicatorstyle:uiactivityindicatorviewstylegray]; [_indicador setcenter:self.view.center]; [_indicador sethideswhenstopped:yes]; [self.view addsubview:_indicador]; [_indicador startanimating]; }
it idea refactor code; example of dry. best way in case use inheritance: write common code in viewcontroller
class, e.g.
viewcontroller.h
@interface viewcontroller : uiviewcontroller { // variables declared here can used in subclasses }
viewcontroller.m
- (void)viewdidload { [super viewdidload]; [self setcandisplaybannerads:yes]; // more common code ... }
and have other view controllers inherit viewcontroller
(rather uiviewcontroller
):
firstviewcontroller.h
@interface firstviewcontroller : viewcontroller ...
firstviewcontroller.m
- (void)viewdidload { [super viewdidload]; // more specific code [[[self.tabbarcontroller.viewcontrollers objectatindex:1] tabbaritem]setimage:[uiimage imagenamed:@"iconobota30x30.png"]]; [[[self.tabbarcontroller.viewcontrollers objectatindex:2] tabbaritem]setimage:[uiimage imagenamed:@"actividades.png"]]; [[[self.tabbarcontroller.viewcontrollers objectatindex:3] tabbaritem]setimage:[uiimage imagenamed:@"nosotros.png"]]; ... }
Comments
Post a Comment