ios - DismissViewController 关闭两个 UIViewController

标签 ios uiview uiviewcontroller presentmodalviewcontroller dismiss

我以这种方式呈现一个 View :

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ContractDetailsViewController *contractDetailsViewController = [[ContractDetailsViewController alloc] init];
    contractDetailsViewController.client = self.client;
    contractDetailsViewController.contractId = [NSString stringWithFormat:@"%d", indexPath.row];

    [self presentViewController:[[UINavigationController alloc]initWithRootViewController:contractDetailsViewController] animated:YES completion:^{
    }];
}

View 出现后,我将其关闭:

- (void)close
{
    [self dismissViewControllerAnimated:YES completion:^{}];
}

错误是 View 消失了,它后面的 View 也随之消失了。

最佳答案

这可以解释您的 View Controller 发生了什么:

If you present several view controllers in succession, thus building a stack of presented view controllers, calling this method on a view controller lower in the stack dismisses its immediate child view controller and all view controllers above that child on the stack. When this happens, only the top-most view is dismissed in an animated fashion; any intermediate view controllers are simply removed from the stack. The top-most view is dismissed using its modal transition style, which may differ from the styles used by other view controllers lower in the stack.

( source )

这意味着如果您呈现 View Controller A,然后是 View Controller B,然后在 View Controller B 上调用 dismiss,则两个 View Controller 都将被关闭(尽管只有 B 带有动画)。

如果你想管理两个不同层次的 View Controller ,你可以使用第二个导航 Controller 。这意味着,例如,您将呈现一个新的导航 Controller 而不是 View Controller A,并使 View Controller A 成为导航 Controller 的根 Controller 。然后你将推送/弹出任何其他所需的 View Controller (B 等)

关于ios - DismissViewController 关闭两个 UIViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21380341/

相关文章:

ios - 如何确定当前在 SwiftUI 中是否选择了 View

ios - 将 tableViewCell 中的数据传递给另一个 VC 失败

ios - 在 uicollectionview 中的单元格下方添加水平线

ios - 调整自定义 UINavigationController 的 titleView

ios - 在 UIView 中从 UIViewController 快速调用组件

iphone - 隐藏的 UITabBar 仍然裁剪放在它上面的任何 UIView

ios - 在 viewWillDisappear 中将委托(delegate)设置为 nil?

ios - Xcode 的 Watchkit 中带有滚动表的静态背景 (Apple Watch)

ios - Swift 中具有两个不同 Segue 的一个原型(prototype)单元格

ios - 当系统清除内存并关闭 UIViewController 时 iOS 崩溃