Question

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.

Was it helpful?

Solution

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];
}

OTHER TIPS

What you want to use instead is

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event

This method Returns the farthest descendant of the receiver in the view hierarchy (including itself) that contains a specified point.

So lets say you have one UITapGapstureRecogniser set on your UIView object and you have tag property set for every imageview :

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewPressed:)];
[_view_1 addGestureRecognizer:tap];


- (IBAction)viewPressed:(UIGestureRecogniser *)sender{
   CGPoint touchPoint = [sender locationInView:self.view];
   UIView *touchView = [self.view hitTest:touchPoint withEvent:nil];
   if(touhView isKindOfClass:[UIImageView class] && touchView.tag == 1)
   {
     // Do action on touch of imageview 1
   } 
   else if(touhView isKindOfClass:[UIImageView class] && touchView.tag == 2) {
    // Do action on touch of imageview 2
   }
   // similarly for other image views
}

This is one solution buts its still not great...

picview_1.tag = 0;
picview_2.tag = 1;
picview_3.tag = 2;
picview_4.tag = 3;
picview_5.tag = 4;
picview_6.tag = 5;
picview_7.tag = 6;
picview_8.tag = 7;
picview_9.tag = 8;

UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imagepressed:)];
[picview_1 addGestureRecognizer:tap1];

UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imagepressed:)];
[picview_2 addGestureRecognizer:tap2];

UITapGestureRecognizer *tap3 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imagepressed:)];
[picview_3 addGestureRecognizer:tap3];

UITapGestureRecognizer *tap4 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imagepressed:)];
[picview_4 addGestureRecognizer:tap4];

UITapGestureRecognizer *tap5 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imagepressed:)];
[picview_5 addGestureRecognizer:tap5];

UITapGestureRecognizer *tap6 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imagepressed:)];
[picview_6 addGestureRecognizer:tap6];

UITapGestureRecognizer *tap7 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imagepressed:)];
[picview_7 addGestureRecognizer:tap7];

UITapGestureRecognizer *tap8 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imagepressed:)];
[picview_8 addGestureRecognizer:tap8];

UITapGestureRecognizer *tap9 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imagepressed:)];
[picview_9 addGestureRecognizer:tap9];

And as for the method:

-(void)imagepressed:(UIGestureRecognizer*)sender {
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];

}

If anyone know how I can improve this that would be great thanks.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top