Navigation through TabbarController And NavigationController. How? - swift

i have storyboard file like this:enter image description here
when user is loggedin i make segue with:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialVC: UITabBarController = storyboard.instantiateViewController(withIdentifier: "loggedin") as! UITabBarController
self.present(initialVC, animated: true, completion: nil)
and automaticly open viewcontroller with identifier "selectsaloon", but sometimes depending on the condition I need to go to viewcontroller with identifier "searchparams". help me find a solution

You can Make navigation on viewdidload for selectsaloon when condition occuer
if (condition) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialVC: UIVieWController = storyboard.instantiateViewController(withIdentifier: "searchparams") as! UIViewController
self.navigationController.push(initialVC, animated: true)
}

Related

How can i return to Main.storyboard which contains a TabBarController from another Storyboard

I have two storyboards, one which contains everything the app really is, and another which contains an "onboarding"/tutorial to my app.
Once the tutorial is done, I want to navigate back to my original view controller.
This is my code to navigate to the other storyboard:
let defaults = UserDefaults.standard
if defaults.bool(forKey: "firstOpened") {
print("First VC launched")
}else{
var vc: UIViewController
let goTo = UIStoryboard(name: "Onboarding", bundle: nil).instantiateViewController(withIdentifier: "notificationStoryboard") as! FirstOnboardingViewController
self.present(goTo, animated: true, completion: nil)
}
With this, it works, except the TabBarController is not showing, and not working the way I want it to.
And this is my code to navigate back to the Main.Storyboard:
#objc func handleSecondPush() {
//registerForRemoteNotifications()
UserDefaults.standard.set(true, forKey: "firstOpened")
let goTo = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "pushToFeedVC")
self.present(goTo, animated: true, completion: nil)
//self.performSegue(withIdentifier: "goToLink", sender: nil)
}
I have also tried this in the other Storyboard, but with this the button doesn't change the view:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "MainTabBarController") as! UITabBarController
print(controller)
self.window?.rootViewController = controller
self.window?.makeKeyAndVisible()
if let tabBarVc = self.window?.rootViewController as? UITabBarController {
tabBarVc.selectedIndex = 1
}
Question in short terms: So my question is, how can I navigate back to the main.storyboard which will contain the TabBarController with a selected index of 1, from a storyboard that doesn't contain navigation controller or a TabBarController?
When you present the onbaording you should return back to the tab with with
self.dismiss(animated:true,completion:nil)
for complex presentations you can do this for easy re-back
let vc = storyboard.instantiateViewController(withIdentifier: "MainTabBarController") as! UITabBarController
(UIApplication.shared.delegate as! AppDelegate).window!.rootViewController = vc
What is better is to
let goTo = UIStoryboard(name: "Onboarding", bundle: nil).instantiateViewController(withIdentifier: "notificationStoryboard") as! FirstOnboardingViewController
let nav = UINavigationController(rootViewController:goTo)
nav.isNavigationBarHidden = true
self.present(nav, animated: true, completion: nil)
the flow inside the onbarding should be with pushViewController
then to dismiss in the last onbaording vc
if let tab = (UIApplication.shared.delegate as! AppDelegate).window?.rootViewController as? UITabBarController {
tab.dismiss(animated:true,completion:nil)
}

Show or open or nivigate to UITabBarController form Viewcontroller like login page in swift

My request is very simple and I am new in ios developing,
I want to show UITabBarController after login successfully
#IBAction func LoginAction(_ sender: Any) {
//correct me here!
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let tabbar:UITabBarController? = (storyBoard.instantiateViewController(withIdentifier: "LiveViewController") as? UITabBarController)
navigationController?.pushViewController(tabbar!, animated: true)
}
main.storyBoard storyBoard:
This
navigationController?
is nil in
navigationController?.pushViewController(tabbar!, animated: true)
You need
let tabbar = storyboard!.instantiateViewController(withIdentifier: "LiveViewController") as! UITabBarController
let nav = UINavigationController(rootViewController: tabbar)
nav.isNavigationBarHidden = true
(UIApplication.shared.delegate as! AppDelegate).window!.rootViewController = nav

Present ViewController from UNUserNotificationCenterDelegate

In my UNUserNotificationCenterDelegate method I want to jump the user to a specific view controller. I can easily get the VC itself from the storyboard:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "beach")
What I'm not entirely sure of though is now how to actually present it, since I don't have an object handy to call presentViewController on.
If your root is navigationVC
if let nav = self.window?.rootViewController as? UINavigationController {
nav.pushViewController(vc, animated: true)
}

Open the VC by pressing a button from the XIB

Help me please.
There is a map with markers, clicking on them brings up a XIB = UIVIew, there is a button in it, you need to click the button to open the viewController
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "MarkerVC")
self.navigationController?.pushViewController(vc, animated: true)
let viewController = YourViewController(nibName: "YourViewController", bundle: nil)
self.present(viewController, animated: true, completion: nil)
If the VC is from XIB, use the above.

How to correctly display a tab controller in swift

I'm making a login screen for my app and everything works as intented until I try to present my main view after the login(which uses a Tab Bar Controller).
The only problem is that it displays just the first item on the tab bar. I have to press the other buttons for the to appear.
Im using this code:
//after login...
var storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var vc: TabBarViewController = storyboard.instantiateViewControllerWithIdentifier("MainController") as! TabBarViewController
self.presentViewController(vc, animated: true, completion: nil)
My guess is that I need to load them all at the same time, but I dont know...
This is how I did it recently. I loaded my tabBarController and the login screen together, once the user has logged in (or completed the first screen experience) you can modally dismiss the controller.
func showloginView() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let loginViewController: LoginTableViewController = storyboard.instantiateViewControllerWithIdentifier("LoginTVC") as! LoginTableViewController
self.window?.makeKeyAndVisible()
var viewController = storyboard.instantiateViewControllerWithIdentifier("LoginTVC") as! LoginTableViewController
let nav = UINavigationController(rootViewController: viewController)
self.window?.rootViewController?.presentViewController(nav, animated: true, completion: nil)
}
for displaying your tabBarController and editing of any of the tabBarItems
let tabBarController = self.window?.rootViewController as? UITabBarController
let tabBarRootViewControllers: Array = tabBarController!.viewControllers!
let nav = tabBarRootViewControllers[0] as? UINavigationController
Hope this helps =)
If you link an UIButton, you can open a UITabBarController using this (providing you're using a Storyboard)
#IBAction func openTabBar(sender: AnyObject) {
var tabBarController = self.storyboard?.instantiateViewControllerWithIdentifier("tabBarController") as! UITabBarController
self.presentViewController(tabBarController, animated: false, completion: nil)
}
That'll pop the current view and open the tab bar controller.