ios - Too much boiler plate code - Methods -


i have simple uiviewcontroller 9 uiimageviews. when each uiimageview pressed method (or function) called. works fine problem have boiler plate code now.

in viewdidload method have 9 uitapgesturerecognizer detect when of 9 uiimageviews pressed. call method run. here code:

uitapgesturerecognizer *tap1 = [[uitapgesturerecognizer alloc] initwithtarget:self action:@selector(imagepressed1:)]; [picview_1 addgesturerecognizer:tap1];  uitapgesturerecognizer *tap2 = [[uitapgesturerecognizer alloc] initwithtarget:self action:@selector(imagepressed2:)]; [picview_2 addgesturerecognizer:tap2];  uitapgesturerecognizer *tap3 = [[uitapgesturerecognizer alloc] initwithtarget:self action:@selector(imagepressed3:)]; [picview_3 addgesturerecognizer:tap3];  uitapgesturerecognizer *tap4 = [[uitapgesturerecognizer alloc] initwithtarget:self action:@selector(imagepressed4:)]; [picview_4 addgesturerecognizer:tap4];  uitapgesturerecognizer *tap5 = [[uitapgesturerecognizer alloc] initwithtarget:self action:@selector(imagepressed5:)]; [picview_5 addgesturerecognizer:tap5];  uitapgesturerecognizer *tap6 = [[uitapgesturerecognizer alloc] initwithtarget:self action:@selector(imagepressed6:)]; [picview_6 addgesturerecognizer:tap6];  uitapgesturerecognizer *tap7 = [[uitapgesturerecognizer alloc] initwithtarget:self action:@selector(imagepressed7:)]; [picview_7 addgesturerecognizer:tap7];  uitapgesturerecognizer *tap8 = [[uitapgesturerecognizer alloc] initwithtarget:self action:@selector(imagepressed8:)]; [picview_8 addgesturerecognizer:tap8];  uitapgesturerecognizer *tap9 = [[uitapgesturerecognizer alloc] initwithtarget:self action:@selector(imagepressed9:)]; [picview_9 addgesturerecognizer:tap9]; 

and here methods being called:

-(void)imagepressed1:(uigesturerecognizer*)sender { imageviewer *screen = [[imageviewer alloc] initwithnibname:nil bundle:nil]; self.seconddata = screen; seconddata.page_num = page; screen.modaltransitionstyle = uimodaltransitionstylecrossdissolve; [self presentviewcontroller:screen animated:yes completion:nil]; }  -(void)imagepressed2:(uigesturerecognizer*)sender {     imageviewer *screen = [[imageviewer alloc] initwithnibname:nil bundle:nil];     self.seconddata = screen;     seconddata.page_num = page + 1;     screen.modaltransitionstyle = uimodaltransitionstylecrossdissolve;     [self presentviewcontroller:screen animated:yes completion:nil]; }  -(void)imagepressed3:(uigesturerecognizer*)sender {     imageviewer *screen = [[imageviewer alloc] initwithnibname:nil bundle:nil];     self.seconddata = screen;     seconddata.page_num = page + 2;     screen.modaltransitionstyle = uimodaltransitionstylecrossdissolve;     [self presentviewcontroller:screen animated:yes completion:nil]; }  -(void)imagepressed4:(uigesturerecognizer*)sender {     imageviewer *screen = [[imageviewer alloc] initwithnibname:nil bundle:nil];     self.seconddata = screen;     seconddata.page_num = page + 3;     screen.modaltransitionstyle = uimodaltransitionstylecrossdissolve;     [self presentviewcontroller:screen animated:yes completion:nil]; }  -(void)imagepressed5:(uigesturerecognizer*)sender {     imageviewer *screen = [[imageviewer alloc] initwithnibname:nil bundle:nil];     self.seconddata = screen;     seconddata.page_num = page + 4;     screen.modaltransitionstyle = uimodaltransitionstylecrossdissolve;     [self presentviewcontroller:screen animated:yes completion:nil]; }  -(void)imagepressed6:(uigesturerecognizer*)sender {     imageviewer *screen = [[imageviewer alloc] initwithnibname:nil bundle:nil];     self.seconddata = screen;     seconddata.page_num = page + 5;     screen.modaltransitionstyle = uimodaltransitionstylecrossdissolve;     [self presentviewcontroller:screen animated:yes completion:nil]; }  -(void)imagepressed7:(uigesturerecognizer*)sender {     imageviewer *screen = [[imageviewer alloc] initwithnibname:nil bundle:nil];     self.seconddata = screen;     seconddata.page_num = page + 6;     screen.modaltransitionstyle = uimodaltransitionstylecrossdissolve;     [self presentviewcontroller:screen animated:yes completion:nil]; }  -(void)imagepressed8:(uigesturerecognizer*)sender {     imageviewer *screen = [[imageviewer alloc] initwithnibname:nil bundle:nil];     self.seconddata = screen;     seconddata.page_num = page + 7;     screen.modaltransitionstyle = uimodaltransitionstylecrossdissolve;     [self presentviewcontroller:screen animated:yes completion:nil]; }  -(void)imagepressed9:(uigesturerecognizer*)sender {     imageviewer *screen = [[imageviewer alloc] initwithnibname:nil bundle:nil];     self.seconddata = screen;     seconddata.page_num = page + 8;     screen.modaltransitionstyle = uimodaltransitionstylecrossdissolve;     [self presentviewcontroller:screen animated:yes completion:nil]; } 

as can see, methods pretty same except 1 small detail, integer called "page" being increased different number depending on function.

is there way can achieve same functionality above without unprofessional copies of code?

thanks, dan.

put picview_n in array, , add different recognizer them in loop. give each picview_n tag corresponds number want added page_num, , use sender.view.tag find number @ runtime:

nsarray *picviews = @[picview_1, picview_2, picview_3, picview_4, picview_5, picview_6, picview_7, picview_8, picview_9]; nsuinteger tag = 1; (uiview *picview in picviews) {     picview.tag = tag++;     uitapgesturerecognizer *tap = [[uitapgesturerecognizer alloc] initwithtarget:self action:@selector(imagepressed:)];     [picview addgesturerecognizer:tap]; } ... -(void)imagepressed:(uigesturerecognizer*)sender { // common recognizers     imageviewer *screen = [[imageviewer alloc] initwithnibname:nil bundle:nil];     self.seconddata = screen;     seconddata.page_num = page + sender.view.tag;     screen.modaltransitionstyle = uimodaltransitionstylecrossdissolve;     [self presentviewcontroller:screen animated:yes completion:nil]; } 

Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -