iOS - double UITabBarControllerDelegate cancels behavior - ios

I have a Swift application with 4 bottom tabs. In the home tab I have a video running. In the 4th tab I have a favorites list.
When the user changes tabs, if he's on the home screen, the video should stop. Also, when the user taps on the 4th tab, the favorites list should update so that the user can see the recent additions.
I have the following on the home tab view controller:
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
print("TAB CHANGED")
if let jwp = jwPlayer {
jwp.stop()
}
}
and this on the favorites tab view controller:
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
let tabBarIndex = tabBarController.selectedIndex
if tabBarIndex == 3 {
let userId = UserDefaults.standard.integer(forKey: "userId")
favoritesVC.updateData(with: userId)
}
}
Both conform to UITabBarControllerDelegate. Both include:
self.tabBarController?.delegate = self
When I launch the app, the video starts playing, I tap on any tab and the video stops. No matter which tab I tap on I see the message "TAB CHANGED". But as soon as I tap on the favorites tab and I move to another, I stop seeing the "TAB CHANGED" message. If I then move to the home screen and play the video and then move to a different tab, the video no longer stops.
The didSelect on the 4th tab is cancelling the didSelect on the 1st tab.
How can I get both of them to work? I have placed them on both view controllers because on the first one I need to reference the video and on the 2nd one I need to reference the list view controller (the 4th view controller actually has two top tabs which switch between a favorites vc and a downloads vc).
UPDATE:
I moved the delegate to the TabBarController subclass. I added the following:
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
let tabBarIndex = tabBarController.selectedIndex
let hvc = HomeViewController()
if tabBarIndex != 0 {
hvc.stopPlayer()
}
}
I added
delegate = self
and
class TabBarViewController: UITabBarController, UITabBarControllerDelegate {
In the HomeViewController I added this:
func stopPlayer() {
print("TAB CHANGED")
if let jwp = jwPlayer {
jwp.stop()
}
}
however when I change tabs, jwPlayer is always nil.

It seems like you are handling the UITabBarControllerDelegate calbacks in multiple places and for that to happen, you are also changing following multiple times -
self.tabBarController?.delegate = self
Here's what you should do -
Handle UITabBarControllerDelegate in one place.
Dispatch the necessary work calls from there to relevant screens.
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
if tabBarIndex == 0 {
homeVC.startPlayer()
}
else {
homeVC.stopPlayer()
if tabBarIndex == 3 {
let userId = UserDefaults.standard.integer(forKey: "userId")
favoritesVC.updateData(with: userId)
}
}
}
This can be handled anywhere, inside your TabBarController subclass if you have one, or any other object that is guaranteed to exist for the application lifetime.

TabBarController’s delegate does not have to be either of the view controllers. It could be some other object (not even a view controller) that could hold weak references to both view controllers, for example, or post a notification etc

Related

getting current active viewController in UITabBarControllerDelegate

I want to implement a scrollToTop method on all of my viewControllers in my UITabBarController. The following is a method in the UITabBarControllerDelegate and triggers, when I select a tab.
The problem is, that I only want to scroll to the top of the viewController, when the viewController is active. So that the user can switch tabs without losing the scroll position, but when he touches the tab in the tabBar of the currently active tab, it should scroll to the top.
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
if viewControllerThatIsCurrentlyActiveInTabBar == viewController {
scrollToTop()
}
}
Basically, I need that condition of the if statement above.
I tried: viewController.isViewLoaded, tabBarController.selectedViewController == viewController, viewController.isBeingPresented. None of those conditions worked. It would either not trigger scrollToTop() or it would trigger always so that you lose the scroll position when you change tabs because it would immediately scroll to the top.
You need to make a code in should select instead of didselect. As it is unable to find the previous controller after selection. below is the example code for it.
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
if tabBarController.selectedViewController == viewController {
print("Same viewcontroller")
}
return true
}
Can you use below extension for getting top viewcontroller of tabbarcontroller.
extension UIViewController {
var top: UIViewController? {
if let controller = self as? UINavigationController {
return controller.topViewController?.top
}
if let controller = self as? UISplitViewController {
return controller.viewControllers.last?.top
}
if let controller = self as? UITabBarController {
return controller.selectedViewController?.top
}
if let controller = presentedViewController {
return controller.top
}
return self
}
}
You can use above extension below
if let rootViewController = UIApplication.top() {
//do with Active view controller
}

DidSelect TabBar Item Not Being Called

So I have a UITabBarController with two View Controllers Embedded into it. I implemented the did select tab bar method where when the user selects a tab, it passes a value into that controller. However when the tabBarController loads for the first time, the did select method is not called even though I have
self.selectedIndex = 0
Which selects the first index. Basically I am just trying to automatically select the first tab Bar Item when the view loads, and have it call the didSelectTabBarItem method
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
}
The question is similar to this one.
tabBarController didSelect does not get called
optional func tabBarController(_ tabBarController: UITabBarController,
didSelect viewController: UIViewController)
In iOS v3.0 and later, the tab bar controller calls this method regardless of whether the selected view controller changed. In addition, it is called only in response to user taps in the tab bar and is not called when your code changes the tab bar contents programmatically.
Copy this in your code
class HomeTabBarVC: UITabBarController {
var isIpad = false
let button = UIButton.init(type: .custom)
var index = 0
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
APPDELEGATE.tabBar = self
}
}
extension HomeTabBarVC : UITabBarControllerDelegate {
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
print("the last selected index is : \(selectedIndex)")
APPDELEGATE.tabBarLastSelectedIndex = selectedIndex
print("the current selected index is : \(String(describing: tabBar.items?.index(of: item)))")
APPDELEGATE.tabBarCurrentSelectedIndex = tabBar.items?.index(of: item) ?? 0
}
}
In appdelegate declare this variable,
var tabBar : UITabBarController?

Dismiss View Controller inside UITabController

I've a project which contains a tab controller, as the main 'page'.
I want to add a UITabBar button item, which presents a view controller modaly, and within that view controller, add a dismiss button that dismisses that view controller and returns to the previous tab selection.
To clarify, it's something that the Medium app for iOS uses, when you click on the create post item, it presents it modaly and dismisses it when you want to.
I can present the view controller, but I can't dismiss it.
Hope I made myself understandable.
Example:
So I've managed to solve this as I wanted to, hope the answer will help someone in the future.
First of all, I set on my first loaded view controller, lets say my 'Home' view controller the tabBarController delegate to be my AppDelegate.swift file (this is personal preference), as:
self.tabBarController?.delegate = UIApplication.shared.delegate as? UITabBarControllerDelegate
Then in my AppDelegate.swift file i added to the class properties the delegate UITabBarControllerDelegate so I could access the tabBarController delegate functions, such as
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {}
Then edited the function to interact with my specific view controller.
The final version of the function:
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
if viewController is ViewControllerToPresentModaly {
if let newVC = tabBarController.storyboard?.instantiateViewController(withIdentifier: "modalVC") {
tabBarController.present(newVC, animated: true)
return false
}
}
return true
}
Here if you want the regular tab behaviour, return true or your own return false
;)

How to pop to root after double tapping a tab bar item?

Normally when a UINavigationController is placed in a UITabBarController the navigation controller pops to root with the respective tab it is in is double tapped. How do I achieve the same effect with a UISplitViewController is between the tab bar controller and the navigation controller? Ideally it would recurse through the view controller's child view controllers and calling popToRootViewController on all navigation controllers that it finds. Do I have to add my own gesture recognizer to the tab bar since it doesn't look like there is a hook for knowing when a user has double tapped a tab?
In Swift 4, it can go something like this:
class TabBarViewController: UITabBarController, UITabBarControllerDelegate {
private var shouldSelectIndex = -1
override func viewDidLoad() {
super.viewDidLoad()
delegate = self
}
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
shouldSelectIndex = tabBarController.selectedIndex
return true
}
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
if shouldSelectIndex == tabBarController.selectedIndex {
if let splitViewController = viewController as? UISplitViewController {
if let navController = splitViewController.viewControllers[0] as? UINavigationController {
navController.popToRootViewController(animated: true)
}
}
}
}
}
Instead of setting up a UIGestureRecognizer I simply keep track of the selected index in shouldSelectViewController and popped to root on my master navigation controller in didSelectViewController if the old selected index was the same as the new one.

How to redirect to rootviewcontroller on click of Tab bar item in iOS App (Swift)

I am implementing an iOS App with UITabBar with UINavigationViewController in Swift. Now facing an issue,
If I select first tab, I can see 'A' ViewController, and on click of any contents of 'A', I redirect to 'B' UINavigationViewController, Now If I click on Second tab, and then again Clicks the first tab, It is showing 'B' NavigationViewController. Expected is, It should display 'A' ViewController. How to achieve that?
Try implementing didSelectViewController delegate and then on selection of 'A ViewController' index redirect to root viewcontroller.
func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
let index : Int = (tabBarController.viewControllers?.indexOf(viewController))!
if index == 0
{
let navigationController = viewController as? UINavigationController
navigationController?.popToRootViewControllerAnimated(true)
}
}
Download Sample
#IBAction func itemB(sender: UIButton) {
// do something
self.tabBarController?.selectedIndex = 0
}
In Swift 3.1
Add UITabBarControllerDelegate to your TabBar Class:
class YourClass: UITabBarController, UITabBarControllerDelegate {
After:
override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
let yourView = self.viewControllers![self.selectedIndex] as! UINavigationController
yourView .popToRootViewControllerAnimated(false)
}

Resources