我在控制器中声明了一个 void touchesbegan 方法,但它不起作用!我不知道为什么。我有一个图像视图,我计划单击它以移动到下一个控制器。所以我设置了触摸开始方法。我已经在 xib 文件中链接了图像视图,但是当我单击图像时。什么都没发生。请帮忙,谢谢。
视图控制器.h
#import <UIKit/UIKit.h>
@interface imageViewViewController : UIViewController
{
IBOutlet UIImageView *testing;
}
@property(nonatomic, retain) IBOutlet UIImageView *testing;
@end
视图控制器.m
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
if(touch.view == testing)
{
TestViewController *testviewcontroller = [[TestViewController alloc]initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:testviewcontroller animated:YES];
}
}
@end
附言
在这里,我尝试了另一种使用点击手势的方法。testing 是我的图像视图的名称。如您所见,我在 imagedittapped 方法中注释掉了 ns 日志。它一直有效。但是,当我尝试将其导航到另一个页面时,它失败了。
- (void)viewDidLoad
{
UITapGestureRecognizer *tapRecognizer;
[testing setTag:0];
[testing setUserInteractionEnabled:TRUE];
tapRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageViewDidTapped:)] autorelease];
tapRecognizer.numberOfTapsRequired = 1;
[testing addGestureRecognizer:tapRecognizer];
[self.view addSubview:testing];
[testing release];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)imageViewDidTapped:(UIGestureRecognizer *)aGesture {
UITapGestureRecognizer *tapGesture = (UITapGestureRecognizer *)aGesture;
UIImageView *tappedImageView = (UIImageView *)[tapGesture view];
switch (tappedImageView.tag) {
case 0:
//NSLog(@"UIImageView 1 was tapped");
[self navigate];
break;
case 1:
NSLog(@"UIImageView 2 was tapped");
break;
default:
break;
}
}
-(void)navigate
{
TestViewController *testviewcontroller = [[TestViewController alloc]initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:testviewcontroller animated:YES];
}