Programmatically navigate to another view controller/scene
Solution 1
I already found the answer
Swift 4
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "nextView") as! NextViewController
self.present(nextViewController, animated:true, completion:nil)
Swift 3
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("nextView") as NextViewController
self.presentViewController(nextViewController, animated:true, completion:nil)
Solution 2
Try this one. Here "LoginViewController" is the storyboardID specified in storyboard.
See below
let secondViewController = self.storyboard?.instantiateViewControllerWithIdentifier("LoginViewController") as LoginViewController
self.navigationController?.pushViewController(secondViewController, animated: true)
Solution 3
XCODE 8.2 AND SWIFT 3.0
Present an exist UIViewController
let loginVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LoginViewController") as! LoginViewController
self.present(loginVC, animated: true, completion: nil)
Push an exist UIViewController
let loginVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LoginViewController") as! LoginViewController
self.navigationController?.pushViewController(loginVC, animated: true)
Remember that you can put the UIViewController
Identifier following the next steps:
- Select
Main.storyboard
- Select your
UIViewController
- Search the Utilities in the right
- Select the identity inspector
- Search in section identity "Storyboard ID"
- Put the Identifier for your
UIViewController
Solution 4
If you want to navigate to Controller created Programmatically, then do this:
let newViewController = NewViewController()
self.navigationController?.pushViewController(newViewController, animated: true)
If you want to navigate to Controller on StoryBoard with Identifier "newViewController", then do this:
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let newViewController = storyBoard.instantiateViewController(withIdentifier: "newViewController") as! NewViewController
self.present(newViewController, animated: true, completion: nil)
Solution 5
Swift3:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController("LoginViewController") as UIViewController
self.navigationController?.pushViewController(vc, animated: true)
Try this out. You just confused nib with storyboard representation.
Related videos on Youtube
Nurdin
My Profile - http://www.revivalx.com Data Consultant at Deepagi - https://deepagi.com KLSE & Crypto screener - https://deepagiscreener.com Github - https://github.com/datomnurdin Linkedin - http://www.linkedin.com/pub/mohammad-nurdin/57/290/692 Youtube - https://www.youtube.com/channel/UCP2lj6CO3Grss4v2ebJ1C1A #SOreadytohelp
Updated on October 14, 2020Comments
-
Nurdin over 3 years
I got an error message during navigating from first view controller to second view controller. My coding is like this one
let vc = LoginViewController(nibName: "LoginViewController", bundle: nil) self.navigationController?.pushViewController(vc, animated: true)
The problem is I always got this kind of error message
2014-12-09 16:51:08.219 XXXXX[1351:60b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </var/mobile/Applications/FDC7AA0A-4F61-47E7-955B-EE559ECC06A2/XXXXX.app> (loaded)' with name 'LoginViewController'' *** First throw call stack: (0x2efcaf0b 0x39761ce7 0x2efcae4d 0x31b693f9 0x31ac1eaf 0x3191e365 0x317fe895 0x318a930d 0x318a9223 0x318a8801 0x318a8529 0x318a8299 0x318a8231 0x317fa305 0x3147631b 0x31471b3f 0x314719d1 0x314713e5 0x314711f7 0x3146af1d 0x2ef96039 0x2ef939c7 0x2ef93d13 0x2eefe769 0x2eefe54b 0x33e6b6d3 0x3185d891 0x4ccc8 0x4cd04 0x39c5fab7) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)
-
gabbler over 9 yearsYou need to add a
LoginViewController.xib
file in your project. When you createLoginViewController.swift
, make sure include xib is selected. -
gabbler over 9 yearsTry to add a complete new view controller and substitute
LoginViewController
and see if it works. -
Nurdin over 9 yearsBTW, I'm using storyboard
-
gabbler over 9 yearsMe too, I am using storyboard and used your code to load a xib file and it worked for me.
-
Noah almost 8 yearsTo access a storyboard in your code, you need to set its Story Board ID in your storyboard.
-
-
Michael Brown almost 9 yearsFrom within a ViewController, you can access the storyboard using self.StoryBoard
-
DanielZanchi about 8 yearsit hides the "navigation bar"
-
Fever over 7 years@Danny182 If you want navigation bar to show, use
self.navigationController?.pushViewController(nextViewController, animated: true)
-
Sparrow about 7 yearsYou need to add some content around your answer and explain it a little bit.
-
Louis Langholtz about 7 yearsIs there any introduction or explanation you can think of for the code in your answer? Perhaps a first sentence like "I think you want code like this:".
-
Allison over 6 yearsI was surprised that Nurdin's answer was suggested to work (and it didn't) because it was just calling push. This of course LOOKS like it should work since we're dealing with the navigation controller
-
Yucel Bayram over 6 yearsTrue answer, i voted up but why we need "Restoration ID" ? I believe "Storyoard ID" is enough for this situation.
-
Cristian Mora over 6 yearsThank for your feedback @yucelbayram A restoration identifier is a string that you need to assign to any view controller or view that you want preserved and restored. In my case, i use this field because i need for determinate function however if you isn't need it can you remove.
-
Lucas over 5 yearsIt should be
storyboard
-
kiran over 5 yearsWhat if its have UINavigationController tag to UITabbarViewController how to push UINavigationController in that case?
-
Jonas Deichelmann over 4 yearsThank you. I tried to use the method for a xib, instead I needed the one for a Storyboard. Your answer helped me a lot!
-
subair_a over 4 yearsIt(Swift 4) presents the new view controller modally