I am opening new viewcontroller like
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let view_controller1 = storyBoard.instantiateViewController(withIdentifier: "incident_view_id") as! IncidentViewController
let appDelegate = UIApplication.shared.delegate as! AppDelegate
//self.present(registerViewController, animated: true)
let navigationController = UINavigationController.init(rootViewController: view_controller1)
appDelegate.window?.rootViewController = navigationController
Which works fine,
Now when I press a button I need to close current view and go back previous view, I tired
self.navigationController?.popToRootViewController(animated: true)
But not works, I tried this too
self.view.window!.rootViewController?.dismiss(animated: true, completion: nil)
But same result.
You are presenting your ViewController in a wrong way. The following line tells the application to no matter what ViewControllers you have opened, just throw it out and assign the UINavigationController what you are creating, as the rootViewController. So the reason your code, the dismiss, is not working because there are no other ViewControllers behind your current one.
appDelegate.window?.rootViewController = navigationController // Wrong
This usually goes into the beginning of the applications, where you define with which UIViewController you want to start your application. I believe you are calling the first code chunk you posted from a UIViewController. So what you need to do instead of creating a new UINavigationController and assigning it to the rootViewController, you just use the UIViewControllers navigation controller to push the next UIViewController.
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let view_controller1 = storyBoard.instantiateViewController(withIdentifier: "incident_view_id") as! IncidentViewController
navigationController?.pushViewController(view_controller1, animated: true)
I am having issues using pushViewController in Swift5, I have searched StackOverflow for answers & could not find any one that works.
All I'm trying to do is navigate to another controller using pushViewController.
Using self.present works for Navigation but pushViewController does not work, for reasons I do not know.
ONLY Using present works below:
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let viewController =
storyBoard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
self.present(viewController, animated: true, completion: nil)
But using pushViewController never works:
I tried this: I doesn't work
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
navigationController?.pushViewController(vc, animated: true)
I tried this: I doesn't work too
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "ViewController" )
var navigationController = UIApplication.shared.keyWindow?.rootViewController as! UINavigationController
navigationController.pushViewController(viewController, animated: true)
I tried this, It also doesn't work
let viewController = storyboard.instantiateViewController(withIdentifier: "ViewController" )
let vc = self.windowRootViewController() as! ViewController
let nvc = vc.parent as! UINavigationController
nvc.pushViewController(viewController, animated: true)
Please can someone guide me properly on what I'm doing wrong or checklist or configuration I need to do for this to work?
Thanks everyone!
Set breakpoint in your code and check whether navigationController is not nil. If it is nil, pushViewController will not work.
Since you are able to present the viewController, instantiating itt using "ViewController" should not be an issue.
The ViewController which try to push must be embedded into NavigationController.
Assume you have two ViewControllers - ViewControllerA and ViewControllerB.
In order to push ViewControllerB from ViewControllerA, The ViewControllerA must be embedded into NavigationController.
So that you can push ViewControllerB to the navigation stack.
If ViewControllerA is not embedded into NavigationController It's navigationController property will be nil. So you will not be able to push ViewControllerB.
I am trying to present the account viewcontroller on other storyboard. But when i present it, the the Tabbar doesn't shows up.
This is my code on the other storyboard that i would like when a button is pressed on the other storyboard it goes to the account viewcontroller (See on the picture) Sorry for my english. Thanks
let storyboard : UIStoryboard = UIStoryboard(name: "UnitedArabEmirates", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "UAE")
self.present(vc, animated: true)
Edit: Sorry for the wrong question. Let me rephrase it. Sorry i am new to programming and swift.
This is my 1st storyboard that when i tapped the "Upload CV" it goes to the the 2nd storyboard and presents my AccountViewController or as it is named identifier as "Account" but when i try to present it, the tabbar that contains the 3 tabbar items doesn't shows up. Just the navigationbar. How can i present the AccountViewController that has a the 3 tabbar items. Thank you so much for your responds.
Here is my first storyboard
Here is my second storyboard
and here is the code that presents my AccountViewController when i tapped the upload CV
let storyboard : UIStoryboard = UIStoryboard(name: "UnitedArabEmirates", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "Account")
self.present(vc, animated: true)
let storyboard : UIStoryboard = UIStoryboard(name: "UnitedArabEmirates", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "UAE")
self.navigationcontroller.present(vc, animated: true)
Please try this.
Give ViewController as a NavigationController then present it. It will show the tab bar.
let vc : YourViewController = self.storyboard!.instantiateViewController(withIdentifier: "UAE")as! YourViewController
let navController = UINavigationController(rootViewController: vc) // Creating a navigation controller with VC1 at the root of the navigation stack.
self.present(navController, animated: true, completion: { _ in })
I'm using following code to programmatically navigate to another ViewController. It works fine, but it some how hides the navigation bar. How do I fix this? (the navigation bar is created by embeding the ViewController in the navigation controller if that matters.)
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("nextView") as NextViewController
self.presentViewController(nextViewController, animated:true, completion:nil)
Swift 5
The default modal presentation style is a card. This shows the previous view controller at the top and allows the user to swipe away the presented view controller.
To retain the old style you need to modify the view controller you will be presenting like this:
newViewController.modalPresentationStyle = .fullScreen
This is the same for both programmatically created and storyboard created controllers.
Swift 3
With a programmatically created Controller
If you want to navigate to Controller created Programmatically, then do this:
let newViewController = NewViewController()
self.navigationController?.pushViewController(newViewController, animated: true)
With a StoryBoard created Controller
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)
SWIFT 4.x
The Strings in double quotes always confuse me, so I think answer to this question needs some graphical presentation to clear this out.
For a banking app, I have a LoginViewController and a BalanceViewController. Each have their respective screens.
The app starts and shows the Login screen. When login is successful, app opens the Balance screen.
Here is how it looks:
The login success is handled like this:
let storyBoard: UIStoryboard = UIStoryboard(name: "Balance", bundle: nil)
let balanceViewController = storyBoard.instantiateViewController(withIdentifier: "balance") as! BalanceViewController
self.present(balanceViewController, animated: true, completion: nil)
As you can see, the storyboard ID 'balance' in small letters is what goes in the second line of the code, and this is the ID which is defined in the storyboard settings, as in the attached screenshot.
The term 'Balance' with capital 'B' is the name of the storyboard file, which is used in the first line of the code.
We know that using hard coded Strings in code is a very bad practice, but somehow in iOS development it has become a common practice, and Xcode doesn't even warn about them.
You should push the new viewcontroller by using current navigation controller, not present.
self.navigationController.pushViewController(nextViewController, animated: true)
According to #jaiswal Rajan in his answer. You can do a pushViewController like this:
let storyBoard: UIStoryboard = UIStoryboard(name: "NewBotStoryboard", bundle: nil)
let newViewController = storyBoard.instantiateViewController(withIdentifier: "NewViewController") as! NewViewController
self.navigationController?.pushViewController(newViewController, animated: true)
So If you present a view controller it will not show in navigation controller. It will just take complete screen. For this case you have to create another navigation controller and add your nextViewController as root for this and present this new navigationController.
Another way is to just push the view controller.
self.presentViewController(nextViewController, animated:true, completion:nil)
For more info check Apple documentation:-
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/#//apple_ref/doc/uid/TP40006926-CH3-SW96
OperationQueue.main.addOperation {
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let newViewController = storyBoard.instantiateViewController(withIdentifier: "Storyboard ID") as! NewViewController
self.present(newViewController, animated: true, completion: nil)
}
It worked for me when I put the code inside of the OperationQueue.main.addOperation, that will execute in the main thread for me.
All other answers sounds good, I would like to cover my case, where I had to make an animated LaunchScreen, then after 3 to 4 seconds of animation the next task was to move to Home screen. I tried segues, but that created problem for destination view. So at the end I accessed AppDelegates's Window property and I assigned a new NavigationController screen to it,
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let homeVC = storyboard.instantiateViewController(withIdentifier: "HomePageViewController") as! HomePageViewController
//Below's navigationController is useful if u want NavigationController in the destination View
let navigationController = UINavigationController(rootViewController: homeVC)
appDelegate.window!.rootViewController = navigationController
If incase, u don't want navigationController in the destination view then just assign as,
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let homeVC = storyboard.instantiateViewController(withIdentifier: "HomePageViewController") as! HomePageViewController
appDelegate.window!.rootViewController = homeVC
The above code works well but if you want to navigate from an NSObject class, where you can not use self.present:
let storyBoard = UIStoryboard(name:"Main", bundle: nil)
if let conVC = storyBoard.instantiateViewController(withIdentifier: "SoundViewController") as? SoundViewController,
let navController = UIApplication.shared.keyWindow?.rootViewController as? UINavigationController {
navController.pushViewController(conVC, animated: true)
}
I want to segue from AppDelegate to First View of UITabBarController.
I assigned StoryBoardID of UITabBarController as "HomePage".
and I tried below code:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController: UITabBarController = storyboard.instantiateViewControllerWithIdentifier("HomePage") as! UITabBarController
let rootViewController = self.window!.rootViewController as! UINavigationController
rootViewController.pushViewController(viewController, animated: true)
It works but I gave BackBarButton on destination view which I don't want.
I want to segue Presently.
You need to set your viewControllers instead of pushViewController
rootViewController.viewControllers = [viewController]
Instead of
rootViewController.pushViewController(viewController, animated: true)