How to correctly display a tab controller in swift - 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.

Related

Tab bar and Navigation bar disappeared when navigate from Appdelegate

I use Storyboard. When I tried to navigate to another View from AppDelegate, the Tab bar and Navigation bar in that View were disappeared
Here is the code
//Some conditions here
let storyBoard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let homeView = storyBoard.instantiateViewController(withIdentifier: "HomeViewController")
self.window?.rootViewController?.present(homeView, animated: true, completion: nil)
Select ViewController from the storyboard.
Go to the Editor and Embed with Navigation Controller or Tab Bar Controller
Give Storyboard ID to your Navigation Controller or Tab Bar Controller
Assign that Navigation Controller or Tab Bar Controller to Root Viewcontroller from AppDelegate.
let storyBoard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let Root_Vc = storyBoard.instantiateViewController(withIdentifier: "RootVc")
self.window?.rootViewController?.present(Root_Vc, animated: true, completion: nil)
You are just loading the viewcontroller on to the rootview. That's why you can't see tabbar or Navigation bar. You need to present Tabbar or navigation controller in order to see it.
let storyBoard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let homeView = storyBoard.instantiateViewController(withIdentifier: "HomeViewController")
self.window?.rootViewController = UINavigationController(rootViewController: homeView)
use this code it should work for you

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)
}

UINavigationController to UITabBarController

The first part of my app is a Sign in section with built in Navigation controller. When the sign up section is completed and a new user is created I would like to proceed into a UITabBarController and will add seperate navigation controllers for each tab. Right now when sign up is complete and my UITabBarController is presented it still shows the UINavigationController from the first section of my app. How do I exit the UINavigationController once I enter the new UITabBarController?
And here is the code
import UIKit
class SignUpSecondViewController: UIViewController, BEMCheckBoxDelegate {
#IBAction func completeButtonAction(_ sender: Any) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "NewTabBarViewController") as! UIViewController
// Alternative way to present the new view controller
self.navigationController?.show(vc, sender: nil)
}
You should better use
self.navigationController?.present(vc, animated: true, completion: nil)
Generally there is no need for signup once main tabbar controller is up. In this case you can do this which will remove signup from memory.
let appDelegate = UIApplication.shared.delegate as! AppDelegate)
appDelegate.window?.rootViewController = vc