从当前和推送的视图中返回按钮处理

时间:2017-03-07 08:40:54

标签: ios objective-c iphone uinavigationcontroller presentviewcontroller

我有一个UIViewControllerC,它是从另外两个UIViewcontroller

调用的

ViewControllerA ViewControllerB

从ViewControllerA转到UIViewControllerC我必须使它成为presentviewcontroller

 UIViewControllerC *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"UIViewControllerC"];
    [vc setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
    vc.tagfrom=@"present";
    [self presentViewController:vc animated:NO completion:nil];

从ViewControllerB转到UIViewControllerC我必须使其推送视图

UIViewControllerC *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"UIViewControllerC"];
        [vc setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
        vc.tagfrom=@"push";
        [self.navigationController pushViewController:vc animated:YES];

现在我必须从后退按钮的两个视图返回我检查tagfrom条件并处理它

-(IBAction)backBtn:(id)sender
{
 if ([tagfrom isEqualToString:@"present"])
  {
    [self dismissViewControllerAnimated:NO completion:nil];
}
else
{
[self.navigationController popViewControllerAnimated:YES];
}

} 

哪个在senerio中工作正常,但有时我的推送视图表现得像presentmodelveiw,并且没有过渡效果,请帮我解决

{{3}}

1 个答案:

答案 0 :(得分:3)

首先,您的tagfrom子类不需要UIViewController属性。

UIViewController个实例有一个名为presentingViewController的属性,可让您了解当前{{1}的viewController A,在您的情况下) ( C在你的情况下)。

viewController

其次,-(IBAction)backBtn:(id)sender { if (nil != self.presentingViewController) { [self dismissViewControllerAnimated:NO completion:nil]; } else { [self.navigationController popViewControllerAnimated:YES]; } } 应该只与 modalTransitionStyle NOT presentation转换一起使用。如果您在push / pop中使用此行为,则行为未定义,因为它不适合在那里使用。