解除通过presentViewController加载的2个UIViewController

时间:2016-03-08 18:26:04

标签: ios objective-c iphone uiviewcontroller

当用户点击按钮时,我显示UIViewController,如下所示:

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:

现在,UIViewController显示了presentViewController: {/ 1}},

当用户点击按钮时,我需要这两个ViewControllers来解除。我怎么能这样做?

我尝试了以下代码,但它没有用。

[self.navigationController popToRootViewControllerAnimated:YES]; 

1 个答案:

答案 0 :(得分:0)

您需要使用[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
    }];
}];