Swift: How to Execute an Action When Uitabbaritem Is Pressed

Perform Action When TabBarItem is Pressed

You need to create a custom tab controller file and assign it as a custom class to your tab controller. Here's an example:

import UIKit

class CustomTabViewController: UITabBarController,UITabBarControllerDelegate {

override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
print("Selected item", item.tag )
}

func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
print("Selected view controller", viewController)
print("index", tabBarController.selectedIndex )

}

}

How would I perform an action when a UITabBarItem is clicked in Swift?

I solved it by putting the update code in the viewDidAppear() method

Detect when a tab bar item is pressed

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, something like this:

class MyTabBarController: UITabBarController, UITabBarControllerDelegate {

then, in that class, override viewDidLoad and in there set the delegate property to self:

self.delegate = self

Note: This is setting the tab bar controller delegate. The tab bar has it's own delegate (UITabBarDelegate), which the tab bar controller manages, and you are not allow to change.

So, 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:

// UITabBarDelegate
override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
print("Selected item")
}

// UITabBarControllerDelegate
func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
print("Selected view controller")
}

I'm guessing you're probably more interested in the latter. Check out the documentation to see what each of these delegates provide.

Last thing, in your storyboard (assuming you are using storyboards), set your tab bar controller's class to MyTabBarController in the Identity Inspector, and you're good to go.

Swift 3/4

// UITabBarDelegate
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
print("Selected item")
}

// UITabBarControllerDelegate
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
print("Selected view controller")
}

How to do an action when tab bar item is pressed every time Swift

You probably just need to put the reload call in the viewWillAppear of the viewController with the table you want to reload, then, every time it is presented it will reload the data.

Anyway, if you want to catch the tap on of the tabs, you need to subclass UITabBarController and then implement this method -(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item

how to call a function from a view controller from UITabBarControllerDelegate when a tab bar item is pressed?

You need to get a reference to the view controller so you can call the function in it.

Quick example, assuming you have setup in Storyboard a TabBarController, assign its Custom Class as MyCustomTabBarController, with two tabs - FirstTabVC and SecondTabVC:

class MyCustomTabBarController: UITabBarController, UITabBarControllerDelegate {

override func viewDidLoad() {
super.viewDidLoad()
delegate = self
}

// this is called *when* the tab item is selected (tapped)
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {

// safely unwrap optionals
guard let theItems = self.tabBar.items,
let idx = theItems.firstIndex(of: item),
let controllers = self.viewControllers
else { return }

if let vc = controllers[idx] as? FirstTabVC {
vc.someFunctionInFirst("From didSelect item")
}
if let vc = controllers[idx] as? SecondTabVC {
vc.someFunctionInSecond("From didSelect item")
}

}

// this is called when the tab's ViewController is selected (*after* the tab item is selected (tapped))
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
if let vc = viewController as? FirstTabVC {
vc.someFunctionInFirst("From didSelect viewController")
}
if let vc = viewController as? SecondTabVC {
vc.someFunctionInSecond("From didSelect viewController")
}
}

}

class FirstTabVC: UIViewController {
public func someFunctionInFirst(_ str: String) {
print("In First Tab VC: ", str)
}
}
class SecondTabVC: UIViewController {
public func someFunctionInSecond(_ str: String) {
print("In Second Tab VC: ", str)
}
}

swift when I try to make an action with my tab bar item, I can only make an outlet or an outlet collection how can I make an action?

I found the answer!

first I ran the code in the viewdidload but in the viewdidload code only loads (runs) one time so if you want to run it every time you go to the tabbar you need to paste the same code inside the viewwillappear:

override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
}

if you run your code in the viewWillAppear than it will load every time you go to the tabbar

I hope this helps the people that also have this question.



Related Topics



Leave a reply



Submit