0

我在控制器中声明了一个 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];
}
4

1 回答 1

1

问题是userInteractionEnabledUIImageView 默认为 NO,因此您不会有任何接触。将其设置为是。

消息处理touchesBegan也非常复杂。如果您将 UITapGestureRecognizer 附加到图像视图,您会更开心。

编辑:现在你说你的触摸处理正在工作,但导航没有发生。因此,让我们专注于代码的这一部分:

-(void)navigate
{
    TestViewController *testviewcontroller = [[TestViewController alloc]initWithNibName:nil bundle:nil];

    [self.navigationController pushViewController:testviewcontroller animated:YES];
}

在那里登录以确保navigate被调用!如果它没有被调用,你需要弄清楚为什么你的其他代码没有运行并调用它。如果它调用,那么问题很可能self.navigationController是 nil,即您一开始不在导航界面内。

于 2013-04-04T03:24:13.657 回答