Search code examples
iosswiftstoryboardxib

how to call xib file from storyboard on button click in swift


I am new in swift I want to call .xib file from storyboard on button click. I am using code like this but it crash because .xib file is not in storyboard

 let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
 let newViewController = storyBoard.instantiateViewController(withIdentifier: "abcViewController") as! abcViewController
 self.present(newViewController, animated: true, completion: nil)

this code

 let newViewController = abcViewController(nibName: "abcViewController", bundle: nil)
 self.present(newViewController, animated: true, completion: nil)

screen like this enter image description here

I just change line and now its work fine

 let newViewController = abcViewController(nibName: "abcViewController", bundle: nil)
 //self.present(newViewController, animated: true, completion: nil)

 self.navigationController?.pushViewController(newViewController, animated: true)

Solution

  • let newViewController = abcViewController(nibName: "xibFileName", bundle: nil)
    //If you want to present xib
    self.present(newViewController, animated: true, completion: nil)
    
    //If you want to push xib in navigation stack
    self.navigationController?.pushViewController(newViewController, animated: true)