13

Hi I'm trying to present a viewcontroller and dismiss my current modal view but this code is not working

self.dismissViewControllerAnimated(true, completion: {
    let vc = self.storyboard?.instantiateViewControllerWithIdentifier("OrderViewController")
    self.presentViewController(vc!, animated: true, completion: nil)
})

vice versa is not working too on completion block of presentviewcontroller

EDIT: replaced vc! to self

pacification
  • 5,838
  • 4
  • 29
  • 51
Ron Pelayo
  • 655
  • 2
  • 7
  • 22

5 Answers5

19

You have to get the viewController which presented self (current ViewController). If that view controller is rootViewController, then you can use the code below, if not then query it based on your view controller hierarchy.

if let vc3 = self.storyboard?.instantiateViewController(withIdentifier: "vc3") as? ViewController3 {
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    appDelegate.window?.rootViewController!.present(vc3, animated: true, completion: nil)
}
TenaciousJay
  • 6,750
  • 3
  • 45
  • 48
Sandeep Kumar
  • 889
  • 7
  • 24
  • thanks! your code works well and this http://stackoverflow.com/questions/26667009/get-top-most-uiviewcontroller aswell – Ron Pelayo Jun 12 '16 at 07:00
8

you can't do that because when the UIViewController A calls the UIViewController B and the first controller is dismissed then the two controllers are nil.

You need to have a UIViewController as a base, in this case MainViewController is the base. You need to use a protocol to call the navigation between controllers.

you can do using protocol let say for example as bellow:-

In to your viewController setting Protocol :

    protocol FirstViewControllerProtocol {
    func dismissViewController()
}

class FirstViewController: UIViewController {
    var delegate:FirstViewControllerProtocol!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    @IBAction func goBack(sender: AnyObject) {
        self.dismissViewControllerAnimated(true) { 
            self.delegate!.dismissViewController()
        }
    }

Now in your main view controller

class MainViewController: UIViewController, FirstViewControllerProtocol {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


@IBAction func goToFirstViewController(sender: AnyObject) {
    let viewController = self.storyboard?.instantiateViewControllerWithIdentifier(String(FirstViewController)) as! FirstViewController
    viewController.delegate = self
    self.presentViewController(viewController, animated: true, completion: nil)
}



//MARK: Protocol
func dismissViewController() {
    if let viewController = self.storyboard?.instantiateViewControllerWithIdentifier(String(SecondViewController)){
        self.presentViewController(viewController, animated: true, completion: nil)
    }
}

Code example with storyboard:


Ethan Parker
  • 2,986
  • 1
  • 23
  • 29
DariusV
  • 2,645
  • 16
  • 21
6

I think there is a mistake in your code where 'self' should be the presenting view controller to present 'vc', not 'vc' its self

Your code

self.dismissViewControllerAnimated(true, completion: {
                let vc = self.storyboard?.instantiateViewControllerWithIdentifier("OrderViewController")
                vc!.presentViewController(vc!, animated: true, completion: nil)
            })

Try this

self.dismissViewControllerAnimated(true, completion: {
                let vc = self.storyboard?.instantiateViewControllerWithIdentifier("OrderViewController")
                self.presentViewController(vc!, animated: true, completion: nil)
            })

hope this is helpful

HSG
  • 1,224
  • 11
  • 17
  • Do you check whether somehow vc is nil. Just in case to make sure everything is OK, then we are able to trace the issue. – HSG Jun 12 '16 at 06:00
  • it's not nil and my code now is let vc = self.storyboard?.instantiateViewControllerWithIdentifier("OrderViewController") as! OrderViewController – Ron Pelayo Jun 12 '16 at 06:04
3
let parent = self.parentViewController!

parent.dismissViewControllerAnimated(true, completion: {
            let vc = self.storyboard?.instantiateViewControllerWithIdentifier("OrderViewController")
            parent.presentViewController(vc!, animated: true, completion: nil)
        })
Code
  • 6,041
  • 4
  • 35
  • 75
0

Here's a solution for Swift3

To present the ViewController

let NotificationVC = self.storyboard?.instantiateViewController(withIdentifier: "NotificationVC") as! ExecutiveNotificationViewController

self.present(NotificationVC, animated: true, completion: nil)

To dismiss the ViewController:

self.dismiss(animated: true, completion: nil)
Rob
  • 26,989
  • 16
  • 82
  • 98
pansora abhay
  • 892
  • 10
  • 16