I have two classes AppDelegate.swift and scene.swift and My first screen is login screen when I click login button then it should set my HomeVC.swift as rootViewController at the action of login button I mentioned this code and its not giving any error not even running
let sceneDelegate = SceneDelegate()
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc = mainStoryboard.instantiateViewController(withIdentifier: "HomeVC") as! HomeVC
sceneDelegate.window?.rootViewController = vc
window?.makeKeyAndVisible()
Can somebody guide me how to work with new file sceneDelegate.swift?
Thanks in advance
when I click login button then it should set my HomeVC.swift as rootViewController
let vc = // ...
self.view.window?.rootViewController = vc
I am implementing multiple language support in a app. Can any one say how to reload storyboard to change localised text without restart of app?
To reload storyboard you need to set RootViewController on changing language. You can use bellow line of code to set RootViewController:
let firstVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "FirstViewController")
let rootviewcontroller: UIWindow = ((UIApplication.shared.delegate?.window)!)!
rootviewcontroller.rootViewController = firstVC
If you want to embed navigation controller in firstVC you should use following line of code:
let firstVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "FirstViewController")
let nav = UINavigationController(rootViewController: firstVC)
let rootviewcontroller: UIWindow = ((UIApplication.shared.delegate?.window)!)!
rootviewcontroller.rootViewController = nav
I want to create a Welcome & Guide page for an iOS app. I am using Swift 3. My problem is that when I try to show the Main.storyboard after finishing the Guide instructions, I get a black view instead of the Main.storyboard:
I use window?.rootViewController = ViewController()in AppDelegate.swift to switch a new ViewController which is linked with the Main.storyboard. But I get nothing except a black view.
If you have used storyboard then you will have to fetch your view controller as
let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "your_identifier_which_you_have_set_in_storyboard")
window?.rootViewController = viewController
If your viewController is embedded in UINavigationController then just make this change:
window?.rootViewController = UINavigationController(rootViewController: viewController)
try this one.
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "MainVC")
window?.rootViewController = UINavigationController(rootViewController: viewController)
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'm doing an app which opens a detail view from a push notification.
In my AppDelegate I'm opening the view, but the view doesn't have a navigation controller.
Then I think that my solution is use the method instantiateViewControllerWithIdentifier
to handle the navigation controller, I'm not sure if this is the solution, but here is the question of the title...
My class SlideNavigationController is Objective-C, and when I use the method
let rootVC = UIStoryboard(name: "Main", bundle: nil).
instantiateViewControllerWithIdentifier("SlideNavigationController") as! SlideNavigationController
I get the error:
warning: could not load any Objective-C class information. This will significantly reduce the quality of type information available.
Any idea? Sorry for my English, edits are welcome! Thanks
The problem with my UIController is solved. Here is my code:
let rootViewController = self.window!.rootViewController as! UINavigationController
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let targerView = mainStoryboard.instantiateViewControllerWithIdentifier("view_place_detail") as! VCPlaceDetailTour
targerView.placeId = aps as String
rootViewController.pushViewController(targerView, animated: false)
You can do with this code.
let rootViewController = self.window!.rootViewController as! UINavigationController
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let targerView = mainStoryboard.instantiateViewControllerWithIdentifier("view Identifier") as! PromotionDetailsVC
rootViewController.pushViewController(targerView, animated: false)
I hope this will help you.
You can programatically create a navigation Controller and set the desired view controller as its rootViewController of the navigation controller and then set navigation controller as windows rootViewController.
eg :
let rootVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("SlideNavigationController") as! SlideNavigationController
let window = UIApplication.sharedApplication().keyWindow
window.rootViewController = rootVC
Same is expalined in this link too : set initial viewcontroller in appdelegate - swift
You must need to create a bridging header to use objective c controllers in the swift project.
Follow the link below for information on creating bridging header.
How to find path of Bridging-Header.h - Swift, Xcode
After that you must import the .h files of controllers in the bridging header file.
Now you can easily use your objective c view controller in swift project.