Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Wednesday, March 30, 2016

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

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. This button opens up the UIViewController number 2 and there user has another button. When user presses it - he opens UIViewController number 3. There is also a button and when user presses it - he calls the code:

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?

For now I created a function and call it through protocol:

UIViewController number 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:

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. Is there a way of removing number 2 without showing it to the user and point him directly to the number 1?

Answer by Jeff for is there a way of dismissing two uiViewControllers at the same time in Swift?


You can use popToViewController(viewController: UIViewController, animated: Bool)

Where viewController is the viewController you wish to pop to, in your case, 'UIViewController number 1'.

popToViewController Documentation

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.

Answer by Vladimir Kravchenko for is there a way of dismissing two uiViewControllers at the same time in Swift?


Try this:

  @IBAction func exitbuttonaction(sender: AnyObject) {      let presentingController = self.presentingViewController!      presentingController.view.alpha = 0      self.dismissViewControllerAnimated(true, completion: {        presentingController.dismissViewControllerAnimated(false, completion: nil)      })    }  

Answer by Andrew McKinley for is there a way of dismissing two uiViewControllers at the same time in Swift?


There is a method on UINavigationController called 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.

Answer by ryantxr for is there a way of dismissing two uiViewControllers at the same time in Swift?


You can use removeLast() function to pop controllers off the stack.

@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)          }  

Answer by Chance Hudson for is there a way of dismissing two uiViewControllers at the same time in Swift?


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.

Try putting this method in view controller 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.


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.