Search code examples
swiftseguexib

How do you present a VC from Xib in Swift?


I am trying to present a VC from a Xib file but it's resulting in

Fatal error: Unexpectedly found nil while unwrapping an Optional value

@IBAction func viewCartBtnTapped(_ sender: Any) {

        let cart = storyboard?.instantiateViewController(withIdentifier: "CartVC") as? CartVC

        present(cart!, animated: true, completion: nil)

}

enter image description here


Solution

  • try below code, YourStoryBoradName should be the name of storyboard without ".storyboard" extension :

    let storyBoard : UIStoryboard = UIStoryboard(name: "YourStoryBoradName", bundle:nil)
    let viewController = storyBoard.instantiateViewController(withIdentifier: "CartVC")as! CartVC
    self.present(viewController, animated: true, completion: nil)