0
votes

I have the tabBar below and each of those TabBars belongs to it's own specific UIViewController . My issue is with my Home Tab Bar . That TabBar has a TableView if I am in another TabBar item like 'Search' or 'Notification' and go back to my Home tab Bar item then I want my TableView to remain in the same place . I have been able to do that by calling this

   controller.dismiss(animated: false, completion: nil)

however as you can see from the image below my Home Tab Bar item does not get highlighted and I created a method that attempts to get my Home Tab highlighted but I get a nil exception . First let me show you my custom function all Tab Bar items on click go through here

class HomeProfile: NSObject {
var mycontroller = ControllerEnum()


func TabBarLogic(_ tabBar: UITabBar, didSelect item: UITabBarItem, streamsModel: Int, controller: UIViewController) {

    if tabBar.items?.index(of: item) == 0 {
          tabBar.tintColor = UIColor(hexString: "#004d99")

        if TabBarCounter == 0 {
   // To Refresh 
            let nextViewController = controller.storyboard?.instantiateViewController(withIdentifier: "HomeC") as! HomeC
            controller.present(nextViewController, animated:false, completion: nil)
        } else {
// keeps TableView position but does not highlight Tab           
            controller.dismiss(animated: false, completion: nil)
            let Home = HomeC()
            Home.HighlightTabBar() // Nil exception here
            TabBarCounter = 0
        }

    }
    if tabBar.items?.index(of: item)! == 1 {
        // Search Tab Item
        tabBar.tintColor = UIColor(hexString: "#004d99")
        let nextViewController = controller.storyboard?.instantiateViewController(withIdentifier: "LocalSearchC") as! LocalSearchC
        controller.present(nextViewController, animated:false, completion:nil)
                      TabBarCounter = 1
    }
    if tabBar.items?.index(of: item)! == 2 {
      // Post
    }
    if tabBar.items?.index(of: item)! == 3 {
         // Notification
    }
    if tabBar.items?.index(of: item)! == 4 {
        // Menu
    }

}
  }

This functionality is meant to be similar to facebook, if you are on the HomePage and click the Home Tab again then it will refresh the TableView however if you are on another Tab and go back to the Home tab it'll maintain your position; The TabBarCounter checks on that .

Ok this is my Home Controller code for the Tab

class HomeC: UIViewController,UITableViewDataSource,UITableViewDelegate, UITabBarDelegate{

 @IBOutlet weak var TabBar: UITabBar!

 override func viewDidLoad() {
        super.viewDidLoad()
TabBar.delegate = self
      TabBar.selectedItem = TabBar.items![0]
       TabBar.tintColor = UIColor(hexString: "#004d99")
}

  func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        homeProfile.TabBarLogic(tabBar, didSelect: item, streamsModel: TabCounter,controller: self)

}

func HighlightTabBar() {
    TabBar.delegate = self 
    TabBar.selectedItem = TabBar.items![0] // this fails
    TabBar.tintColor = UIColor(hexString: "#004d99")
}

}

I know that the reason why it fails is because the TabBar in HighlightTabBar() when calling it does not have the reference to the TabBar . How can I make it so that this works . I have been looking at this one How to programmatically change UITabBar selected index after Dismiss? but so far nothing works .

enter image description here

1
The main cause of the fatal error is in this line let Home = HomeC(). You create an new empty HomeC, where its TabBar is nil. But I do not understand the view hierarchy of your code. Are you not using UITabBarController? If so, why?OOPer
You need to make sure tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) is called after viewDidLoadukim

1 Answers

0
votes

Put the code in App delegate or button action

let appDelegate = UIApplication.shared.delegate as! AppDelegate
let tabBarController = appDelegate.window?.rootViewController as! UITabBarController
tabBarController.selectedIndex = 2