I have a simple UIViewController
with 9 UIImageViews
. When each UIImageView
is pressed a method (or function) is called. This all works fine but the problem is that I have too much boiler plate code now.
In my viewDidLoad
method I have 9 UITapGestureRecognizer
to detect when any of my 9 UIImageViews
are pressed. They then call a method to run. Here is my 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 are the methods that are 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 you can see, my methods are all pretty much the same EXCEPT for one small detail, the Integer called "page" is being increased by a different number depending on the function.
Is there any way I can achieve the same functionality as above but without so much unprofessional copies of my code?
Thanks, Dan.
Put picview_N
in an array, and add a different recognizer to them in a loop. Give each picview_N
a tag that corresponds to the number that you want added to the page_num
, and use sender.view.tag
to find that number at runtime:
NSArray *picViews = @[picview_1, picview_2, picview_3, picview_4, picview_5, picview_6, picview_7, picview_8, picview_9];
NSUInteger tag = 1;
for (UIView *picView in picViews) {
picView.tag = tag++;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imagepressed:)];
[picView addGestureRecognizer:tap];
}
...
-(void)imagepressed:(UIGestureRecognizer*)sender { // Common for all 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];
}