iosswiftxcodestoryboarduistoryboardsegue

Handling the Tab Bar Controller View buttons and code


I'm developing an app for my thesis and I'm a little bit confused on what I should do here/where to write the code that I have and that worked before modifying these views.

Basically after the login process I only had a view (I was still testing at that time), now I made a Tab Bar Controller and 3 view under it. Problem is, the logout button I made for them is no longer working cause I don't know where to write the code for the Tab Bar Controller View...

For a normal view you just connect the swift file and it's done and dusted, you write everything you want in there and watch it work with the assistant view, but I'm not familiar with the Tab Bar Controller View...

In the photo I provided there is a logout button that is clicked, so you can see it better.

Logout Button

The code I used for the button when the view worked:

    @IBAction func bottoneLogout(_ sender: UIBarButtonItem) {

    do {
        try Auth.auth().signOut()
        navigationController?.popToRootViewController(animated: true)
    } catch let signOutError as NSError {
        print("Error signing out: %@", signOutError)
    }

    }

I'm also trying to remove the "Back" button, I had the code for it and have the same question for where to place the code to get it working again:

    override func viewDidLoad() {
    super.viewDidLoad()

    title = "Grafici"
    navigationItem.hidesBackButton = true
    }

App Structure: photo


Solution

  • Try setting a class to UITabBarController like:

    class MyTabbarController: UITabBarController {
    
        // MARK: - Lifecycle
    
        override func viewDidLoad() {
            super.viewDidLoad()
        }
    }
    

    Then set the class of the UITabBarController from the storyboard inspector, then try to put the logout logic in the custom class you created.

    enter image description here

    Note that depending on your view hierarchy, you should manage the navigation, that means if your UITabBarController is a child for UINavigationController, you can access the navigation controller from the tabbar reference: tabbarController?.navigationController