辞退这是通过presentViewController
问题描述:
装载2的UIViewController当用户点击一个按钮,我显示UIViewController
如下:辞退这是通过presentViewController
NSString * storyboardName = @"Main";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"newviews"];
[self presentViewController:vc animated:YES completion:nil];
然而,当vc
视图 - 控制成功加载,用户会点击另一个按钮并显示另一个UIViewcontroller
。与前面的代码一样,这也使用presentViewController:
。
现在,有2 UIViewController
多数民众赞成由presentViewController:
显示,
当一个用户点击按钮,我需要这2个ViewControllers解雇。我怎样才能做到这一点 ?
我试过下面的代码,但它没有工作。
[self.navigationController popToRootViewControllerAnimated:YES];
答
您需要使用[self.parentViewController dismissViewControllerAnimated:YES]
,因为您以模态方式呈现它们。关闭viewControllers时,您可以利用parentViewController
属性。
UIViewController *mainController = [UIViewController new];
UIViewController *vcA = [UIViewController new];
UIViewController *vcB = [UIViewController new];
// User taps button to present vcA
[mainController presentViewController:vcA animated:YES completion:nil];
// User taps button to present vcB
[vcA presentViewController:vcB animated:YES completion:nil];
// Dismiss both vcB and vcA
[vcB.parentViewController dismissViewControllerAnimated:YES completion:^{
[vcA.parentViewController dismissViewControllerAnimated:YES completion:^{
// Now you are back on mainController
}];
}];