I am using SWRevealViewController in my app. Everything is fine except one thing.
Suppose there are A,B,C 3 items in my SWRevealViewController Menu. I want to open B item programatically using swift.
UPDATE: CODE
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let sw = storyboard.instantiateViewController(withIdentifier: "Reveal") as! SWRevealViewController
let destinationController = storyboard.instantiateViewController(withIdentifier: "VehicleList") as! VehicleList
let navigationController = UINavigationController(rootViewController: destinationController)
sw.pushFrontViewController(navigationController, animated: true)
You can have access to the main screen controller using frontViewController property of the SWRevealViewController like that:
let navigationController = revealViewController().frontViewController as? UINavigationController
navigationController?.pushViewController(viewController, animated: false)
You can replace the frontViewController using setFront(_:animated:) function of SWRevealViewController like:
revealViewController()?.setFront(viewController, animated: false)
This is complete solution two my question.
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let destinationController = storyboard.instantiateViewController(withIdentifier: "VehicleList") as! VehicleList
let navigationController = revealViewController().frontViewController as? UINavigationController
navigationController?.pushViewController(destinationController, animated: false)
Related
From a button action, I have presented a viewController, from that presented VC, add another button and on that button action added a childView like this -
let vc = UIStoryboard(name: "Profile", bundle: nil).instantiateViewController(withIdentifier: "APPopUpViewControllerViewController") as! APPopUpViewControllerViewController
self.addChild(vc)
vc.view.frame = self.view.frame
self.view.addSubview(vc.view)
vc.didMove(toParent: self)
All are working perfectly till now, but when i try to push a viewController from this child VC , it does not work. How can i push a viewController from a childView ??
Push viewController code -
let storyboard = UIStoryboard(name: "WPLogin", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "WPSigninViewController") as! WPSigninViewController
self.navigationController?.pushViewController(vc, animated: true)
My guess is that self.navigationController is nil, so your optional chaining is failing.
You can try
print(parent)
print(parent?.navigationController)
self.parent?.navigationController?.pushViewController(vc, animated: true)
How do you present your ViewController ? If You have pushed it to a navigation controller, then your code is perfect and it should work I think.
If not, then try to present the WPSigninViewController modally as
let storyboard = UIStoryboard(name: "WPLogin", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "WPSigninViewController") as! WPSigninViewController
self.present(vc, animated: true)
and It should work.
If ViewController is your rootController, you can add a NavigationController to it :
let viewController = ViewController(nibName: nil, bundle: nil)
let navigationController = UINavigationController(rootViewController: viewController)
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window?.rootViewController = navigationController
self.window?.makeKeyAndVisible()
When a push notification is received, I am trying to navigate to a table view controller (ReportTVC). The Hierarchy of the view controllers in my storyboard is as shown below.
TabBarController -> Navigation Controller (Storyboard ID: CasesNavController) -> TableViewController (CasesTVC) -> TableViewController (CaseSummaryTVC) -> TableViewController(ReportTVC)
The ReportTVC is being displayed with the navigation controller as expected, but when I navigate back to the CasesTVC, I should have a TabBar with the tabs, but this is missing.
Can someone please advise how I could resolve this ?
In AppDelegate.swift:
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let navController = mainStoryboard.instantiateViewController as! UINavigationController
let reportTVC = mainStoryboard.instantiateViewController(withIdentifier: "ReportTVC") as! ReportTVC
reportTVC.obtainDoctorReport = true
reportTVC.caseId = caseId
navController.pushViewController(reportTVC, animated: true)
self.window?.rootViewController = navController
self.window?.makeKeyAndVisible()
The problem here is that you're setting the NavigationController to the as rootViewController you need to set the TabBarController as the root.
let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
let navController = mainStoryboard.instantiateViewController(withIdentifier: "Nav") as! UINavigationController
let tabController = mainStoryboard.instantiateViewController(withIdentifier: "Tab") as! UITabBarController
let reportTVC = mainStoryboard.instantiateViewController(withIdentifier: "ReportTVC") as! ReportTVC
reportTVC.obtainDoctorReport = true
reportTVC.caseId = caseId
navController.pushViewController(reportTVC, animated: true)
tabController.setViewControllers([navController], animated: false)
self.window?.rootViewController = tabController
self.window?.makeKeyAndVisible()
You shouldn't forget that you also need the add the other ViewControllers in the TabBarViewController.
I would like to jump from a viewController to the first viewController related to Tab Bar Controller through code.
The tabBarController Scene has storyboard id tabView.
I'm working on this way:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc: UITabBarController!
storyboard.instantiateViewController(withIdentifier: "tabView")
vc=storyboard.instantiateViewController(withIdentifier: "tabView") as! UITabBarController
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
self.present(vc as! UIViewController, animated: true, completion: nil)
}
But it loads only the first viewController (out of 5) without the tab bar related to. How can I solve it?
Oh, this code looks so wrong.
In your storyboard give and "tabView" ID to the TabBarController, not the ViewController inside it.
Why you are double instantiating ViewController? just do it once and assign it to the vc variable.
Why you've created delay before presenting the VC? It's some sort of workaround of something?
Working code:
let vc = storyboard.instantiateViewController(withIdentifier: "tabView") as! UITabBarController
self.present(vc, animated: true)
Use
(1)if you want to navigate from Appdelegate
let appDelegate = UIApplication.sharedApplication.delegate as! AppDelegate
let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
let tabBar = mainStoryboard.instantiateViewControllerWithIdentifier("TabBarController") as! TabBarController
appDelegate.window?.rootViewController = tabBar
appDelegate.window?.makeKeyAndVisible()
(2)if you want to navigate from viewcontroller which has root of navigation
self.navigationController?.pushViewController(tabBar, animated: true)
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)
}
How to i segue over to another UINavigationViewcController programmatically so there is a back button to go to the previous screen. I tried using this code, but it just models over.
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let resultViewController = storyBoard.instantiateViewControllerWithIdentifier("NavViewController") as FirstMemberViewController
self.presentViewController(resultViewController, animated:true, completion:nil)
Try this
// Your controllers
let navigationController = UINavigationController()
let viewController = UIViewController()
// The push
navigationController.pushViewController(viewController, animated: true)
I found the answer...
navigationController!.pushViewController(storyboard!.instantiateViewControllerWithIdentifier("Home") as! UIViewController, animated: true)