简体   繁体   English

有没有办法在Swift中同时解雇两个uiViewControllers?

[英]is there a way of dismissing two uiViewControllers at the same time in Swift?

In my swift app I have a UIViewController with a button. 在我的swift应用程序中,我有一个带按钮的UIViewController。 This button opens up the UIViewController number 2 and there user has another button. 这个按钮打开了UIViewController的编号2,用户有另一个按钮。 When user presses it - he opens UIViewController number 3. There is also a button and when user presses it - he calls the code: 当用户按下它时 - 他打开UIViewController编号3.还有一个按钮,当用户按下它时 - 他调用代码:

self.dismissViewControllerAnimated(false, completion: nil)

and thanks to it the UIViewController number 3 disappears and user sees UIViewController number 2. My question is - is there a way of also dismissing the UIViewController number 2 so that user can come back smoothly from number 3 to number 1? 并且由于它UIViewController 3号消失,用户看到UIViewController 2号。我的问题是 - 是否还有一种解除UIViewController数字2的方法,以便用户可以顺利地从3号回到1号?

For now I created a function and call it through protocol: 现在我创建了一个函数并通过协议调用它:

UIViewController number 2: 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 number 3: UIViewController编号3:

class YourFullRequest: UIViewController{

    var delegateRequest:HandleYourFullRequest?

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

        delegateRequest?.hideContainer()
    }
}

But with that solution when user presses the button - the UIViewController number 3 disappears and UIViewController number 2 appears for a second and disappears then. 但是当用户按下按钮时,使用该解决方案 - UIViewController编号3消失,UIViewController编号2出现一秒钟然后消失。 Is there a way of removing number 2 without showing it to the user and point him directly to the number 1? 有没有一种方法可以删除2号而不向用户显示并直接指向数字1?

I'm still unclear as two which button is wired to which action, but from what I can tell when the dismiss button is pressed on view controller 3 it calls self.dismissViewControllerAnimated(false, completion: nil) in view controller number 2. 我仍然不清楚两个按钮连接到哪个动作,但是从我在视图控制器3上按下解除按钮时可以看出它在视图控制器编号2中调用self.dismissViewControllerAnimated(false, completion: nil)

Try putting this method in view controller 3. 尝试将此方法放在视图控制器3中。

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

This will work assuming that both view controllers are presented and not pushed in something like a navigation controller. 这将假设两个视图控制器都被呈现而不是像导航控制器那样被推送。

You can use removeLast() function to pop controllers off the stack. 您可以使用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)        
}

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

Where viewController is the viewController you wish to pop to, in your case, 'UIViewController number 1'. 其中viewController是你希望弹出的viewController,在你的例子中,是'UIViewController number 1'。

popToViewController Documentation popToViewController文档

If you don't have a reference to the View Controller, you can get it from self.navigationController.viewControllers , it will be the first object in your example. 如果您没有对View Controller的引用,可以从self.navigationController.viewControllers获取它,它将是您示例中的第一个对象。

There is a method on UINavigationController called setViewControllers. UINavigationController上有一个名为setViewControllers的方法。 It takes an array of all the view controllers you want active. 它需要一个您想要活动的所有视图控制器的数组。 You can get all the view controllers on the stack as an array, remove the ones you don't want, then call setViewControllers with the updated array. 您可以将堆栈上的所有视图控制器作为数组获取,删除不需要的视图控制器,然后使用更新的数组调用setViewControllers。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 DismissViewController解雇两个UIViewControllers - DismissViewController dismissing two UIViewControllers 两个独立的UIViewControllers上的UITabBarControllerDelegate-Swift - UITabBarControllerDelegate on two separate UIViewControllers - Swift 同时在屏幕上显示多个UIViewController - Multiple UIViewControllers on screen at the same time 呃,解雇uiviewcontrollers和imagePickerController - Ugh, dismissing uiviewcontrollers and imagePickerController 在两个UIViewControllers之间进行编程转换(Xcode 9,Swift 4) - Programmatically transitioning between two UIViewControllers (Xcode 9, Swift 4) Swift中一个屏幕上的两个UIViewController之间的交互 - Interaction between two UIViewControllers on one screen in Swift 在UINavigationController中关闭视图并同时同步 - Dismissing view in UINavigationController and at the same time syncing 带有swift的依赖注入,带有两个UIViewControllers的依赖图,没有公共父级 - Dependency Injection with swift with dependency graph of two UIViewControllers without common parent 使用Swift和Storyboard在两个UIViewController之间传递数据 - Passing data between two UIViewControllers using Swift and Storyboard 在SpriteKit + Swift中同时移动两个桨 - Move two paddles at same time in SpriteKit + Swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM