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()
Related
I have 3 VC's (VC as in ViewController)
1 AuthViewController which is the entry point for the app and it checks if user is logged in else it displays the sign-in related views
2 HomeViewController is the main interface of the app landing page after the user has logged in
3 ListViewController which displays a list, invoked via a segue on HomeViewController.
I want to enable back navigation between ListViewController and HomeViewController via a Navigation Controller.
How can I achieve this If I have a Navigation controller whose root VC is HomeVC. how can I invoke it from my AuthVC so that it gets invoked with the Navigation controller.
I have tried to invoke the Navigation controller but did not work
I have also tried invoking the HomeVC by
let mainStoryboard = UIStoryboard(name: "Main", bundle: Bundle.main)
if let viewController = mainStoryboard.instantiateViewController(withIdentifier: "mainViewController") as? UIViewController {
viewController.modalPresentationStyle = .overCurrentContext
self.present(viewController, animated: true, completion: nil)
although I was able to invoke the HomeVC could not get the navigation buttons
How can I invoke HomeVC from the AuthVC without loosing the NavigationController
You need 1 navigation controller, not 3.
If you want to push a new controller, then use push method, not present.
The back button gets enabled automatically on the navigation bar if there are controllers in the navigation stack and the current controller is not a root view controller.
When coming from AuthViewController to HomeController, you should change the RootViewController. So that the user cant go back to the Auth screen. That would make no sense.
Once you are on HomeController, you can push other controller using the below code and it will add a back button to your controller automatically.
let mainStoryboard = UIStoryboard(name: "Main", bundle: Bundle.main)
if let viewController = mainStoryboard.instantiateViewController(withIdentifier: "mainViewController") as? UIViewController {
self.navigationController?.pushViewController(vc, animated: true)
}
You can also use the below extension:
extension UIViewController {
func pushVC(storyboardName : String, vcname : String) {
let vc = UIStoryboard.init(name: storyboardName, bundle: Bundle.main).instantiateViewController(withIdentifier: vcname)
vc.hidesBottomBarWhenPushed = true
self.navigationController?.pushViewController(vc, animated: true)
}
func popVC() {
self.navigationController?.popViewController(animated: true)
}
func makeRootVC(storyBoardName : String, vcName : String) {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let vc = UIStoryboard(name: storyBoardName, bundle: Bundle.main).instantiateViewController(withIdentifier: vcName)
let nav = UINavigationController(rootViewController: vc)
nav.navigationBar.isHidden = true
appDelegate.window?.rootViewController = nav // If using XCode 11 and above, copy var window : UIWindow? in your appDelegate file
let options: UIView.AnimationOptions = .transitionCrossDissolve
let duration: TimeInterval = 0.6
UIView.transition(with: appDelegate.window!, duration: duration, options: options, animations: {}, completion: nil)
}
}
I've created a tabbed application and am able to present a view modally using the code below, however, I'm stuck on dismissing the view and displaying the tab bar and first view controller.
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let signUpViewController = storyboard.instantiateViewController(withIdentifier:"SignUpViewController") as! SignUpViewController
self.window?.makeKeyAndVisible()
self.window?.rootViewController = signUpViewController
Inside that signUpViewController do this after giving that tabBarController a storyboard identifier in IB
let tab = storyboard!.instantiateViewController(withIdentifier:"tab")
UIApplication.shared.keyWindow?.rootViewController = tab
OR
(UIApplication.shared.delegate as! AppDelegate).window?.rootViewController = tab
You should present your modal vc over you tab bar vc
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let signUpViewController = storyboard.instantiateViewController(withIdentifier:"SignUpViewController") as! SignUpViewController
signUpViewController.modalPresentationStyle = UIModalPresentationStyle.fullScreen
signUpViewController.modalTransitionStyle = UIModalTransitionStyle.coverVertical
self.window.rootViewController.present(myModalViewController, animated: true, completion: nil)
(if self.window.rootViewController - your tab bar view controller)
And in this case you can use dismiss(animated: true, completion: nil) method in modal vc to dismiss it.
In your TabBarViewController, do the following when presenting SignUpViewController
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier:"SignUpViewController")
viewController.modalPresentationStyle = .overFullScreen
self.presentViewController(viewController, animated:true)
and when dismissing, simply call the dismiss code from SignUpViewController
e.g. dismissViewControllerAnimated
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)
From appDelegate I want to access a view controller which is embedded with UINavigation controller.
How can I do so, knowing that I can directly access that view controller but in this case the navigation bar will not appear, therefore this is not what I want.
I have tried the code found below but it prompts a warning and a black screen is presented.
var storyboard1 = UIStoryboard(name: "Main", bundle: nil)
var viewController: ExamNav_ViewController = storyboard1.instantiateViewControllerWithIdentifier("ExamNav_ViewController") as! ExamNav_ViewController
var rootViewController = self.window!.rootViewController as! UINavigationController
// rootViewController.pushViewController(viewController, animated: true)
rootViewController.presentViewController(viewController, animated: true, completion: nil)
The warning is: Attempt to present PlayMyWay.ExamNav_ViewController: 0x14fd19b50 on UINavigationController: 0x14fd13a90 whose view is not in the window hierarchy!
window = UIWindow(frame: UIScreen.mainScreen().bounds)
let navController: UINavigationController = UIStoryboard(name: "Main", bundle:nil).instantiateViewControllerWithIdentifier("provaNavController") as UINavigationController
window?.rootViewController = navController
window?.makeKeyAndVisible()
More Click here to learn about that
var storyboard = UIStoryboard(name: "Main", bundle: nil)
var objOrderVC = storyboard.instantiateViewControllerWithIdentifier("ExamNav_ViewController") as! ExamNav_ViewController
let navig = UINavigationController(rootViewController: objOrderVC)
navig.setNavigationBarHidden(false, animated: false)
window?.rootViewController = navig