Segue To Navigation Controller in Tab Bar Controller - swift

In my application, I have a default tab selected for my Tab Bar Controller. On the viewDidLoad() of the default tab, I have this:
if (NSUserDefaults.standardUserDefaults().stringForKey("defaultCode") == nil) {
//let navController:UINavigationController = UINavigationController()
//self.presentViewController(navController, animated: true, completion: nil)
self.tabBarController!.selectedIndex = 1
I thought calling selectedIndex would do what I was looking forever; however, it only changes the selected tab in the TabBar, but my view does not change. The area I commented out quickly shows the proper view, then goes to a black screen. The view I am trying to switch to is a Navigation Controller

selectedIndex always be the first and pop/push after selected

Related

How to navigate from popover view controller in swift?

I have a button on a view controller and by clicking that button a view controller appears as a popup. That popup has a tableview and whenever I click on the tableview row I want it to navigate to different view controller. But I think it is not as simple as using this code.
let vc = storyboard?instantiateViewController(identifier: "controller") as! Controller
self.navigationController.pushViewController(popupView, animated: true, completion: nil)
Is there a way to open a view controller from popup view?

Switch tab bar controller view controller after dismissing a modally presented view controller

In my project you can create a post from a modal view.
When the modal view is dismissed (user presses on save post) I want to switch the tab bar controller to the second tab (post feed screen).
This topic is similar to my problem. The only difference being this is presented from a modal view. I can't figure out how to implement it in my code (tab bar is nil)
Switch tab bar programmatically in Swift
I have added 3 images to make this issue clearer
code screenshot
console message
#objc func saveAction(sender: UIButton) {
print ("> save pressed")
print(presentingViewController?.tabBarController)
print(presentingViewController)
presentingViewController?.tabBarController?.selectedIndex = 1
dismiss(animated: true)
}
edit: sorry stack overflow doesn't allow me to add images yet
You can do this using delegate pattern. But if you prefer not to add a delegate for this, you can do as shown below;
You can switch the tabbar by changing the selectedIndex property of tabBarController
if let presenter = presentingViewController as? LibraryViewController {
presenter.tabBarController?.selectedIndex = 1
}
dismiss(animated: true)
If you are presenting the modal on navigation controller in tabbar, use:
if let tabBar = presentingViewController as? UITabBarController {
tabBar.selectedIndex = 1
}
dismiss(animated: true)

How to display the tabs when click the back button from hided tab view controller

I created a tab bar controller and from one tab item I gave segue to the navigation view controller. And I create a some view controllers attached to navigation controller. So in one view controller I don't need a tabs so in that controller I wrote to hide the tab bar controller that is self.tabBarController?.tabBar.isHidden = true.
When I click the back button of navigation controller from hided tab view controller to previous controller, it doesn't show the tab bar items in previous controllers. But I needed tabs in all view controller except in one view controller. Why does it not show the tabs?
This is my story board :
You can try this in the VC that's before the one you hide the tab in
override func viewWillAppear(_ animated:Bool) {
super.viewWillAppear(animated)
self.tabBarController?.tabBar.isHidden = false
}
You can use hidesBottomBarWhenPushedin the view controller which you don't need tabs. Fits your situation.
let controller = ViewControllerTwo()
controller.hidesBottomBarWhenPushed = true
navigationController?.pushViewController(controller, animated: true)
A little more explanation:
self.tabBarController?.tabBar.isHidden = true globally changed the self.tabBarController's property hideTabBar across its children controllers stack.

How to kill the views of a navigation controller?

i have some navigations controller
Navigation Controller 1
Navigation Controller 2
when the user log off (red box), should be back to screen log in (yellow box)
two navigation controllers are involved, the first one that is in the red box must die completely, just like that tab bar that is included.
this is the code of the close session button.
#IBAction func CerrarSesion(_ sender: Any)
{
do
{
try Auth.auth().signOut()
let vc = self.storyboard?.instantiateViewController(withIdentifier: "InicioSesionLogin")
//self.dismiss(animated: true, completion: nil)
self.present(vc!, animated: true, completion: self.borrarUserDefaults)
}
catch let error as NSError
{
print (error.localizedDescription)
}
}
this code returns me to the view of logging in without a issue, but the views that are ahead are still working, I know because there is a view that uses location functions of beacons, and leave a function that prints ("ranging").
In summary: I want to return to the view to log in (red box) and kill all the views that I have in front of me.
the only thing I could do, is to kill the first navigation controller (yellow box) but it returns me to a back view, where you see the gray box.
if the navigation controller is the rootVc , you can reset it's viewcontrollers property
self.navigationController.viewControllers = [VC]
or reset the rootVC
UIApplication.shared.keyWindow?.rootViewController = // VC

Tab Bar Controller isHidden, but stays hidden

Im currently designing an app that utilizes a tab bar controller.
On the messages tab (instant messages), I want the tab bar to disappear whenever a user is having/viewing his/her conversation with another person. To do so I used this:self.tabBarController?.tabBar.isHidden = true
It disables the tabBar, but now the issue is that whenever I hit the back button to return to previous views (embedded in a navigation controller), the tab bar is still hidden. On the other views, I've set tabBar.isHidden = false, but that doesn't seem to fix it and now I can't access any of the other tabs.
My question is: How can I hide the tabBar on one view but keep it visible when I return to previous views?
In TabBar firstViewController
override func viewWillAppear(animated: Bool) {
// Enable TabBar
self.tabBarController?.tabBar.hidden = false
}
In SecondViewController (Pushed from firstViewController)
override func viewDidLoad() {
super.viewDidLoad()
// Disable TabBar
self.tabBarController?.tabBar.hidden = true
}