首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >导航误差飞快3

导航误差飞快3
EN

Stack Overflow用户
提问于 2016-12-27 20:52:45
回答 1查看 205关注 0票数 1

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

下面是实现该功能的代码:

代码语言:javascript
复制
@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.")
    }
}

提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-01-01 18:07:28

如果我正确地理解了您的问题,这就是您的视图控制器层次结构的样子:

用于添加新任务:

导航控制器--(包含)-->“管理模式”--(模态)-->导航控制器--(包含)-->“添加新任务”

用于编辑任务:

导航控制器--(包含)-->“管理模式”--(推送)-->“添加新任务”

问题是你的案子都不起作用。

isPresentingInAddTaskMode

在第一种情况下,我们看到:

代码语言:javascript
复制
let isPresentingInAddTaskMode = presentingViewController is UINavigationController

if isPresentingInAddTaskMode {
    dismiss(animated: true, completion: nil)
}

我认为这将处理涉及模态演示的案件。

这是不可能的,因为presentingViewController是“管理模式”视图控制器,而不是包含它的导航控制器或包含"Add“视图控制器的导航控制器。

另一个问题是,您必须取消包含“Another”的导航控制器,而不是“Another”本身。这意味着,代替

代码语言:javascript
复制
dismiss(animated: true, completion: nil)

你会做的

代码语言:javascript
复制
navigationController?.dismiss(animated: true, completion: nil)

owningNavigationController

在第二种情况下,我们看到:

代码语言:javascript
复制
else if let owningNavigationController = navigationController{
    owningNavigationController.popViewController(animated: true)
}

第一行是有意义的:您正在展开导航控制器。但是,然后在导航控制器上调用popViewController(animated: true)

问题是,模态和推送都涉及一个导航控制器,所以这种情况对两者都适用。

解决方案

您需要使用上述方法形成一个更简单的cancel方法:

代码语言:javascript
复制
@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以及是否是它本身。如果是这样的话,这是一种模式,它会自我否定。如果不是,它是一个推子,你弹出当前的视图控制器。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41352083

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档