Swift: How to execute an action when UITabBarItem is pressed - ios

Currently I have a Tab Bar Controller that is connected to a tableview controller. I'm trying to go to the top of the tableview when I press the tab bar item. I know how to get to the top of the tableview. I just don't know how to do an action when the item is pressed.

You should use UITabBarDelegate with method didSelectItem. Use it as any standard delegate:
class yourclass: UIViewController, UITabBarDelegate {
func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
//This method will be called when user changes tab.
}
}
And do not forget to set your tab bar delegate to self in view controller.

Here is an answer to this question
Basically you do this:
Make sure your view controller is subscribed to the UITabBarDelegate
Set tags in IB for each tab bar item
Implement the didSelectItem method, something like this:
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
if(item.tag == 1) {
// Code for item 1
}
else if(item.tag == 2) {
// Code for item 2
}
}
This will give you access to each tab item tapped event. Hope it helps!
In Swift:
func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
if(item.tag == 1) {
// Code for item 1
} else if(item.tag == 2) {
// Code for item 2
}
}

SWIFT 3
class yourclass: UIViewController, UITabBarDelegate {
func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
print("Test")
}
}
And do not forget to set your tabBar delegate to self in viewDidLoad
override func viewDidLoad(){
super.viewDidLoad()
<YOUR TAB BAR NAME>.delegate = self
}

I was having trouble implementing the other answers here. This is a fuller answer. It assumes you are using a UITabBarController (the default if you create a new Tabbed App). This solution will print a message every time a view controller tab button is tapped.
Code
Create a new Swift file called MyTabBarController.swift. Paste in the following code.
import UIKit
class MyTabBarController: UITabBarController, UITabBarControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// tell our UITabBarController subclass to handle its own delegate methods
self.delegate = self
}
// called whenever a tab button is tapped
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
if viewController is FirstViewController {
print("First tab")
} else if viewController is SecondViewController {
print("Second tab")
}
}
}
Interface Builder
On your storyboard select the Tab Bar Controller. Then in the Identity inspector, set the class name to MyTabBarController (that is, the name of the class in the code above).
That's all. You can run your app now and be notified whenever the user taps a tab bar item.
Notes
If you need to run a method on a tap, then you can do something like the following in didSelect method.
if let firstVC = viewController as? FirstViewController {
firstVC.doSomeAction()
}
You could do make the FirstViewController implement the delegate and handle everything there. That way you wouldn't need to make any custom UITabBarController subclass and set it in IB. However, having a child do the parent's work seems like the wrong place to do it. Anyway, here is is:
class FirstViewController: UIViewController, UITabBarControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
tabBarController?.delegate = self
}
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
// ...
}
}
The didSelect method above gets called no matter which tab is tapped.
UITabBarControllerDelegate documentation

An alternate solution is to just do something in viewDidAppear in whichever View Controller the tab shows.
First Tab View Controller
import UIKit
class FirstViewController: UIViewController {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
print("First tab")
}
}
Second Tab View Controller
import UIKit
class SecondViewController: UIViewController {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
print("Second tab")
}
}

class TestViewController: UIViewController,UITabBarDelegate {
#IBOutlet weak var tabbar: UITabBar!
override func viewDidLoad() {
super.viewDidLoad()
tabbar.delegate = self
}
func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
print(tabBar.items![1]) // The number is tab index
}
}

Related

iOS: How to open 0th Index of tabBarItem by clicking 3rd index of tabBarItem

I have 5 tabBarItem in my UITabBarController
One scenario, I have to open First index of UITabBarItem by clicking third UITabBarItem
My approach as in below:
extension FiveTabbarController: UITabBarControllerDelegate {
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
if item == (self.tabBar.items!)[2] {
tabBar.selectedItem = (self.tabBar.items!)[0] // ERROR
self.selectedIndex = 0 // NOT WORKING
}
}
}
Error: *** Terminating app due to uncaught exception
'NSInternalInconsistencyException', reason:
'Directly modifying a tab bar managed by a tab bar controller is not allowed.'
Kindly guide me how to achieve this.
You don't want your view controller's base class to be a UITabBarDelegate. If you were to do that, all of your view controller subclasses would be tab bar delegates. What I think you want to do is to extend UITabBarController
class FiveTabbarController: UITabBarController, UITabBarControllerDelegate {
then, in that class, override viewDidLoad and in there set the delegate property to self:
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
}
now this class is both a UITabBarDelegate (because UITabBarController implements that protocol), and UITabBarControllerDelegate, and you can override/implement those delegate's methods as desired, such as:
extension FiveTabbarController: UITabBarControllerDelegate {
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
if let getSelectedIndex = tabBarController.viewControllers?.firstIndex(of: viewController), getSelectedIndex == 2 {
self.selectedIndex = 0
}
}
}
You can achieve the behavior by doing:
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
if selectedIndex == 2 {
self.selectedIndex = 0
}
}
The reason for the error is because you shouldn't modify the selectedItem, only the index. By modifying the index the tab bar controller will set the selectedItem.
Edited
Didn't notice you are using tabBar's method instead of tabbarcontroller's.

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?

Prevent a tabBar moving to the next View Controller onselect

I have an item on my tabBar which I don't want to actually move to its view controller but instead when that item is clicked something happens (a popup dialog appears over the current view controller).
I currently have the current code:
class TabViewController: UITabBarController, UITabBarControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// tell our UITabBarController subclass to handle its own delegate methods
self.delegate = self
}
// called whenever a tab button is tapped
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
if viewController is PostTabViewController {
... code here ...
}
}
}
The code at ..code here... runs just fine however the PostTabViewController is still shown. How would I go about stopping it?
You should do your checks in tabBarController(_, shouldSelect:)
func tabBarController(UITabBarController, shouldSelect: UIViewController) -> Bool {
guard viewController is PostTabViewController else {
return true
}
... code here ...
return false
}

iOS on tab bar change

I want to know when the tab in the tab bar changes, so that i can report it to Firebase Analytics. How do i do this.
I tried this
override func viewDidAppear(_ animated: Bool) {
Analytics.logEvent("projects_open", parameters: [:])
}
But i have a feeling that what would also run when i go back to it from another ViewController. I need something that can detect when a tab is opened, not when it becomes visible.
Is there another func that works for this?
Swift 3.0
Use this two delegate methods, and don't forget to assign delegate to self.
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
//MARK: - UITabBarControllerDelegate
}
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
//MARk: - UITabBarDelegate
}
There is a delegate function on UITabbarController for detecting that a tab was selected:
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController)
You can also access the selected index like this:
tabController.selectedIndex
If you use only tabBar.in viewDidLoad set tabBar delegate to self and
override func tabBar(_ tabBar: UITabBar, didSelect item:UITabBarItem)
{
//MARk: - UITabBarDelegate
if(tabBar.selectedIndex == 0) {
//Do something
}
else if(tabBar.selectedIndex == 1) {
//Do something.
}
}
and if you use tabBarController use this method.And mark delegate as self
func tabBarController(_ tabBarController: UITabBarController,
didSelect viewController: UIViewController) {
}
Very important Note:
If you want to save which tabBar was previously selected you have to save it on your way.Either use flag or NSUserDefaults according to your wish. The reason i mentioned this because i needed to check which tab has been selected right now in View in one of my project.
Swift 5
Easy way just click on link StakOverFlow screen will open
https://stackoverflow.com/a/60539396/6881070

How do I call something in my UIViewController in the code of my UITabBarViewController?

I want to call a method on a ViewController (type "MatchesViewController", which is on the 3rd tab of my TabBarViewControlleer) if the selected item is NOT of that class.
This is my delegate function for listening when a Tab Bar Item was changed.
override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem!) {
}
Inside this code, I'd like to detect if the item selected (and its View Controller) is of type "MatchesViewController". If it is NOT of this type, then call method on that controller.
Why not use tabBarController(_:didSelectViewController:) ?
override func tabBarController(tabBarController: UITabBarController,
didSelectViewController viewController: UIViewController)
{
if !(viewController is MatchesViewController) {
let matchesVC = tabBarController.viewControllers?[2] as MatchesViewController
matchesVC.refresh()
}
}

Resources