我试图实现cancel bar按钮,正如您从图像中可以看到的那样,该按钮返回到以前的viewController,使用dismiss,但是当我单击该按钮时,什么都没有出现,您知道为什么吗?

下面是实现该功能的代码:
@IBAction func cancel(_ sender: UIBarButtonItem) {
let isPresentingInAddTaskMode = presentingViewController is UINavigationController
if isPresentingInAddTaskMode {
dismiss(animated: true, completion: nil)
}
else if let owningNavigationController = navigationController{
owningNavigationController.popViewController(animated: true)
}
else {
fatalError("The AddTaskVC is not inside a navigation controller.")
}
}提前谢谢。
发布于 2017-01-01 18:07:28
如果我正确地理解了您的问题,这就是您的视图控制器层次结构的样子:
用于添加新任务:
导航控制器--(包含)-->“管理模式”--(模态)-->导航控制器--(包含)-->“添加新任务”
用于编辑任务:
导航控制器--(包含)-->“管理模式”--(推送)-->“添加新任务”
问题是你的案子都不起作用。
isPresentingInAddTaskMode
在第一种情况下,我们看到:
let isPresentingInAddTaskMode = presentingViewController is UINavigationController
if isPresentingInAddTaskMode {
dismiss(animated: true, completion: nil)
}我认为这将处理涉及模态演示的案件。
这是不可能的,因为presentingViewController是“管理模式”视图控制器,而不是包含它的导航控制器或包含"Add“视图控制器的导航控制器。
另一个问题是,您必须取消包含“Another”的导航控制器,而不是“Another”本身。这意味着,代替
dismiss(animated: true, completion: nil)你会做的
navigationController?.dismiss(animated: true, completion: nil)owningNavigationController
在第二种情况下,我们看到:
else if let owningNavigationController = navigationController{
owningNavigationController.popViewController(animated: true)
}第一行是有意义的:您正在展开导航控制器。但是,然后在导航控制器上调用popViewController(animated: true)。
问题是,模态和推送都涉及一个导航控制器,所以这种情况对两者都适用。
解决方案
您需要使用上述方法形成一个更简单的cancel方法:
@IBAction func cancel(_ sender: UIBarButtonItem) {
guard let owningNavigationController = navigationController else {
fatalError("The AddTaskVC is not inside a navigation controller.")
}
if owningNavigationController.presentingViewController?.presentedViewController == owningNavigationController {
// modal
owningNavigationController.dismiss(animated: true, completion: nil)
} else {
// push
owningNavigationController.popViewController(animated: true)
}
}这第一个打开导航控制器和错误,如果没有,就像你最初做的那样。
然后,通过一种比以前更复杂的方式,检查是否发生了模态转换。这将检查导航控制器的presentingViewController以及是否是它本身。如果是这样的话,这是一种模式,它会自我否定。如果不是,它是一个推子,你弹出当前的视图控制器。
https://stackoverflow.com/questions/41352083
复制相似问题