【问题标题】:Dismiss 2 UIViewController that was loaded via a presentViewController关闭通过 presentViewController 加载的 2 个 UIViewController
【发布时间】:2016-03-08 18:26:04
【问题描述】:

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

NSString * storyboardName = @"Main";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"newviews"];
[self presentViewController:vc animated:YES completion:nil];

然而,当vc viewcontroller 加载成功时,用户将点击另一个按钮并显示另一个UIViewcontroller。同样,这也使用了presentViewController:,就像之前的代码一样。

现在,presentViewController: 正在显示 2 个UIViewController

当用户点击一个按钮时,我需要这 2 个 ViewController 来关闭。我该怎么做?

我尝试了以下代码,但没有成功。

[self.navigationController popToRootViewControllerAnimated:YES]; 

【问题讨论】:

    标签: ios objective-c iphone uiviewcontroller


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-13
      相关资源
      最近更新 更多