1

I have a Swift project.

It has a UINavigationViewController inside a UITabBarController. When tapping the tab responsible for showing the Navigation View Controller twice, it jumps back to the root view controller of the Nav.

How can I disable this using swift?

NB. I've seen Objective C implementations using the UITabBarControllerDelegate but I don't think I'm doing the right thing in Swift.

Thanks.

orome
  • 45,163
  • 57
  • 202
  • 418
Orane
  • 2,223
  • 1
  • 20
  • 33
  • 1
    Could you post the objC Code, so I can translate it to Swift for you? – Dennis Weidmann Sep 12 '15 at 12:24
  • @Neo, this is the link http://stackoverflow.com/questions/1849975/prevent-automatic-poptorootviewcontroller-on-double-tap-of-uitabbarcontroller. Im not sure where to put the code or where to add the Delegate. – Orane Sep 12 '15 at 13:28

2 Answers2

2

Swift 3.0

add UITabBarControllerDelegate to master class

override func viewDidLoad() {
    tabBarController?.delegate = self  
}

func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
     _ = navigationController?.popToRootViewController(animated: true)
}
Programer_saeed
  • 133
  • 2
  • 5
0

The proper way to achieve it is to use the tabBarController:shouldSelectViewController: method of the UITabBarControllerDelegate protocol. The problem I could see here is that you are not sure where to set the delegate. There must be no big difference between doing in Objective C or Swift.

Here are a few simple steps you may need to try:

  1. Retrieve the tab bar controller: I don't know your app's UI structure, but you should be able to get the tab bar controller easily from code. It could be a property if you created it programmatically, or merely the key window's rootViewController if you drag & drop it to the main storybard.

  2. Assign the tab bar controller's delegate to an instance of any class you want as long as the class conform to the UITabBarControllerDelegate protocol.

  3. Implement the tabBarController:shouldSelectViewController: method mentioned above to decide what should be shown when a tab is selected.

If you can provide some code, I can also show you how you can make it by example.

Good luck.

Ducky
  • 2,754
  • 16
  • 25