【问题标题】:is there a way of dismissing two uiViewControllers at the same time in Swift?有没有办法在 Swift 中同时关闭两个 uiViewController?
【发布时间】:2016-07-18 14:59:05
【问题描述】:

在我的 swift 应用程序中,我有一个带有按钮的 UIViewController。这个按钮打开了 2 号 UIViewController 并且用户有另一个按钮。当用户按下它时 - 他会打开 3 号 UIViewController。还有一个按钮,当用户按下它时 - 他会调用代码:

self.dismissViewControllerAnimated(false, completion: nil)

多亏了它,3 号 UIViewController 消失了,用户看到了 2 号 UIViewController。我的问题是 - 有没有办法也解除 2 号 UIViewController 以便用户可以从 3 号顺利返回到 1 号?

现在我创建了一个函数并通过协议调用它:

UIViewController 2 号:

protocol HandleYourFullRequest: class {
    func hideContainer()
}


class FullRequest: UIViewController, HandleYourFullRequest{

    func hideContainer(){
       self.dismissViewControllerAnimated(false, completion: nil)
    }

    @IBAction func exitbuttonaction(sender: AnyObject) {
        self.performSegueWithIdentifier("temporarySegue", sender: self)
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if (segue.identifier == "temporarySegue"){


        if let fullRequestDetails = segue.destinationViewController as? YourFullRequest
        {

            fullRequestDetails.delegateRequest = self
        }

    }
    }



}

UIViewController 3 号:

class YourFullRequest: UIViewController{

    var delegateRequest:HandleYourFullRequest?

    @IBAction func exitbuttonaction(sender: AnyObject) {
        self.dismissViewControllerAnimated(true, completion: nil)

        delegateRequest?.hideContainer()
    }
}

但是当用户按下按钮时,使用该解决方案 - UIViewController 3 号消失,UIViewController 2 号出现一秒钟然后消失。有没有办法在不向用户显示的情况下删除数字 2 并直接将他指向数字 1?

【问题讨论】:

    标签: ios swift uiviewcontroller segue uistoryboardsegue


    【解决方案1】:

    你可以使用popToViewController(viewController: UIViewController, animated: Bool)

    viewController 是您希望弹出到的 viewController,在您的情况下是“UIViewController number 1”。

    popToViewController Documentation

    如果您没有对 View Controller 的引用,您可以从 self.navigationController.viewControllers 获取它,它将是您示例中的第一个对象。

    【讨论】:

    • 你能给我一个提示,我怎么能引用self.navigationController.viewControllers的第一个元素?我试过popToViewController(self.navigationController.viewControllers.first: UIViewController, animated: Bool),但在我的情况下会导致一些错误:(
    • @user3766930 在弹出视图控制器并打印出 self.navigationController.viewControllers 数组的内容之前设置断点。在这里看到这一点很有用。您的视图控制器层次结构可能与您描述的不同。
    【解决方案2】:

    UINavigationController 上有一个名为 setViewControllers 的方法。它需要一个包含您想要激活的所有视图控制器的数组。您可以将堆栈上的所有视图控制器作为数组获取,删除不需要的视图控制器,然后使用更新后的数组调用 setViewControllers。

    【讨论】:

    • 感谢您的提示,但是-您能否给我一些示例,可能是我当前的代码-我可以如何使用它?我真的很感激!
    • if let myNav = self.navigationController{ var allvc = myNav.viewControllers allvc.removeLast() allvc.removeLast() myNav.setViewControllers(allvc, animated: true) }
    【解决方案3】:

    您可以使用 removeLast() 函数将控制器从堆栈中弹出。

    @IBAction func doneAction(sender: AnyObject) {
        var vc = self.navigationController?.viewControllers
        // remove controllers from the stack
        vc?.removeLast()
        vc?.removeLast()
        // Jump back to the controller you want.
        self.navigationController?.setViewControllers(vc!, animated: false)        
    }
    

    【讨论】:

    • 嗯我尝试了这个解决方案,但是当我按下按钮时 - 没有任何效果:) 我不确定是否有什么我可以错过的?
    • “没有任何效果”是否意味着没有任何反应?您的按钮是否与功能挂钩?
    • 是的,我只看到我在 var vc = self.navigationController?... 正上方添加的打印消息
    • print(vc)看是否为nil。
    【解决方案4】:

    我仍然不清楚两个哪个按钮连接到哪个操作,但是从我可以看出,当在视图控制器 3 上按下关闭按钮时,它在 2 号视图控制器中调用 self.dismissViewControllerAnimated(false, completion: nil)

    尝试将此方法放入视图控制器 3。

    @IBAction func exitButtonAction(sender: AnyObject) {
        self.presentingViewController?.presentingViewController?.dismissViewControllerAnimated(true, completion: nil);
    }
    

    假设两个视图控制器都被呈现并且没有被推送到导航控制器之类的东西中,这将起作用。

    【讨论】:

      猜你喜欢
      • 2018-07-07
      • 1970-01-01
      • 2013-03-01
      • 2020-09-12
      • 2020-07-23
      • 1970-01-01
      • 2020-12-17
      • 1970-01-01
      相关资源
      最近更新 更多