我有一个简单UIViewController
的 9 UIImageViews
。当每一个UIImageView
被按下时,一个方法(或函数)被调用。这一切都很好,但问题是我现在有太多的样板代码。
在我的viewDidLoad
方法中,我有 9 个UITapGestureRecognizer
来检测我的 9 个中的任何一个UIImageViews
被按下的时间。然后他们调用一个方法来运行。这是我的代码:
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];
以下是被调用的方法:
-(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];
}
如您所见,我的方法几乎都相同,除了一个小细节,称为“页面”的整数根据功能增加了不同的数字。
有什么方法可以实现与上述相同的功能,但没有太多不专业的代码副本?
谢谢,丹。