如何通过点击图像以编程方式进行segue?

时间:2013-07-24 12:49:39

标签: iphone ios objective-c ipad

我已经实现了2个带有图像和tapGestures的ImageViews,这很好用,现在我想要根据我点击的图像来转换到另一个ViewController。我不知道如何声明我的ImageView应该调用哪个ViewController。

所以我的代码是:

-(void) initImageViews{
self.navigationItem.title = @"Menu";
UIImage *serviceImage = [UIImage imageNamed:@"Service.png"];
self.serviceImageView = [[UIImageView alloc] initWithImage:serviceImage];
[self.serviceImageView setFrame:CGRectMake(20, 20, 48,48 )];
[self.view addSubview:self.serviceImageView];
[self.view setBackgroundColor:[UIColor blackColor]];

UIImage *industryImage = [UIImage imageNamed:@"Industry.png"];
self.industryImageView = [[UIImageView alloc] initWithImage:industryImage];
[self.industryImageView setFrame:CGRectMake(73,20,48,48)];
[self.view addSubview:industryImageView];
   UITapGestureRecognizer *singleTap =
    [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];

    [self.industryImageView setUserInteractionEnabled:TRUE];
    [self.serviceImageView setUserInteractionEnabled:TRUE];
    [self.serviceImageView addGestureRecognizer:singleTap];
    [self.industryImageView addGestureRecognizer:singleTap];

 }

    - (void)handleSingleTap:(UITapGestureRecognizer *)gestureRecognizer {

NSLog(@"Tapped!");

 }

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
if ([segue.identifier isEqualToString:@"Service"]) {
    SelectOptionViewController *controller = (SelectOptionViewController*)segue.destinationViewController;
}if ([segue.identifier isEqualToString:@"Industry"]) {
    SelectOptionViewController *controller = (SelectOptionViewController*)segue.destinationViewController;
}
}

}

提前致谢:)

3 个答案:

答案 0 :(得分:2)

[self performSegueWithIdentifier:IdentifierForTheSegueiFromStoryBoard sender:nil];

编辑:seque必须存在于故事板中,并且必须具有标识符。

答案 1 :(得分:2)

如果您不想/需要使用segues,您可以使用:

if (i want controller A) {
    [self presentViewController:ControllerA animated:YES completion:nil];
} else if (i want controller B) 
    [self presentViewController:ControllerB animated:YES completion:nil];
}

答案 2 :(得分:1)

使用标识符在故事板中创建一个segue。然后以编程方式调用这样的segue

[self performSegueWithIdentifier: @"myidentifier" sender: self];