Tab Bar Controller, Home Button

I have a Tab Bar Controller, which refers to two Navigation Controllers, these Navigation Controllers refer each to one view Controller. Form this View Controller you can go to another View Controller. If I switch from the first "Workflow" (e.g. thrird page) to the second and back with the Tab Bar I have to click the Button for the first Workflow twice to get to the initial view. Can I make my program to do that only by one click ? I hope you understand my problem and the structure of the program.
Answered by Claude31 in 650111022
IB means Interface Builder.

I do not understand what you copied on line 4
Code Block
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
let rootView = self.viewControllers
[self.selectedIndex] as! UINavigationController
rootView.popToRootView // here the editor says that it does not work/exist //
}


Here is my code:
Code Block
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
if self.selectedIndex == 0 {
let rootView = self.viewControllers![self.selectedIndex] as! UINavigationController
rootView.popToRootViewController(animated: false)
}
}


Note: it is not a very good idea to start a new line 3. It is better to write on a single line:
Code Block
let rootView = self.viewControllers![self.selectedIndex] as! UINavigationController


And don't forget to unwrap self.viewControllers
Is this your set up ?

               Button A -> Nav A -> View A1 -> View A2 -> View A3
TabBar
               Button B -> Nav B -> View B1 -> View B2 -> View B3

With this, can you explain exactly the sequence:

If I switch from the first "Workflow" (e.g. thrird page) to the second and back with the Tab Bar I have to click the Button for the first Workflow twice to get to the initial view. Can I make my program to do that only by one click ?

  • the first "Workflow" (e.g. thrird page) :

You hit Button A then nav to A3. Correct ?
  • to the second :

how do you do it ? Do you hit button B ?
  • and back with the Tab Bar

back to what ? do you tap button A ?
  • I have to click the Button for the first Workflow twice to get to the initial view

  • Do you mean Button A ?

  • where do you get ? A3 ?

  • where do you want to go ? A1 ?

Going to A3 is the normal behaviour.

To achieve what you want (return to A1):

subclass UITabBarViewController as this and declare the tabBarController in IB to be of this class:
Implement this in the class

Code Block
class TabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
let rootView = self.viewControllers![self.selectedIndex] as! UINavigationController
rootView.popToRootViewController(animated: false)
}
}

If you want to do it only with first tab button (A), test selectedIndex:
Code Block
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
if self.selectedIndex == 0 {
let rootView = self.viewControllers![self.selectedIndex] as! UINavigationController
rootView.popToRootViewController(animated: false)
}
}

If that works, don't forget to close the thread on the correct answer ; other, please explain what you were looking for. Good luck.
Hi, thank you for your answer!
I go from A2-> using TabBar clicking on B -> B1->B2-> usingTabBar clicking on A -> and now get to A2, but I always want my TabBar to go to A1/B1, your first solution might work, but "popToRootViewController" does not work, what do you mean by that ?
I tested in an app, and it works: I go to A1 and B1 when clicking on the tabBar buttons.

When you say it doesn't work:
  • does it crash ?

  • doesn't it go to A2 but to A2 or B2 ?

So please check if
  • you subclassed UITabBarController ?

  • you set the class to the ViewController in IB to TabBarController and not left the default UITabBarController ?

If that works, thanks to close the thread by marking the correct answer.
If not, please show precisely what you coded and what you defined in IB.
Xcode "says" this function does not exist. Where exactly did you place this function? In the TabBarController or in the ViewController from A1?
import UIKit

Code Block
class MyTabBarViewController: UITabBarController {
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        let rootView = self.viewControllers
        [self.selectedIndex] as! UINavigationController
        rootView.popToRootView // here the editor says that it does not work/exist //
    }

I made a class for the tab bar Controller and connected it to the storyboard.

And what do you mean with IB ?
Accepted Answer
IB means Interface Builder.

I do not understand what you copied on line 4
Code Block
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
let rootView = self.viewControllers
[self.selectedIndex] as! UINavigationController
rootView.popToRootView // here the editor says that it does not work/exist //
}


Here is my code:
Code Block
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
if self.selectedIndex == 0 {
let rootView = self.viewControllers![self.selectedIndex] as! UINavigationController
rootView.popToRootViewController(animated: false)
}
}


Note: it is not a very good idea to start a new line 3. It is better to write on a single line:
Code Block
let rootView = self.viewControllers![self.selectedIndex] as! UINavigationController


And don't forget to unwrap self.viewControllers
Thank you! I just made a new line, where no one was requested. Now it works like it should
Tab Bar Controller, Home Button
 
 
Q