我以这种方式呈现一个 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/