Dismiss and Present View Controller in Swift

2019-01-17 23:02发布

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

6条回答
叼着烟拽天下
2楼-- · 2019-01-17 23:14

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

查看更多
啃猪蹄的小仙女
3楼-- · 2019-01-17 23:18
let parent = self.parentViewController!

parent.dismissViewControllerAnimated(true, completion: {
            let vc = self.storyboard?.instantiateViewControllerWithIdentifier("OrderViewController")
            parent.presentViewController(vc!, animated: true, completion: nil)
        })
查看更多
Anthone
4楼-- · 2019-01-17 23:21

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:


查看更多
啃猪蹄的小仙女
5楼-- · 2019-01-17 23:23

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)
查看更多
老娘就宠你
6楼-- · 2019-01-17 23:26
if let vc = storyboard?.instantiateViewController(withIdentifier: "IdOfYourVC") {

    present(vc, animated: true, completion: nil)
}
查看更多
叼着烟拽天下
7楼-- · 2019-01-17 23:28

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

if let vc3 = self.storyboard?.instantiateViewControllerWithIdentifier("vc3") as? ViewController3 {
    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    appDelegate.window?.rootViewController!.presentViewController(vc3, animated: true, completion: nil)
}
查看更多
登录 后发表回答