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")
}
Detecting when tabBar item is pressed when is page
One option is do the refreshing inside viewWillAppear()
method. And the second one is considerably long.
In parent view controller
protocol ParentDelegate {
func refresh()
}
class LandingViewController: UIViewController, UITabBarDelegate {
var delegate: ParentDelegate?
var selectedItem: UITabBarItem!
override func viewDidLoad() {
super.viewDidLoad()
self.tabBar.delegate = self
self.selectedItem = self.tabBar.selectedItem
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "SegueNameForDestinationViewController1" {
if let vc = segue.destinationViewController as? YourDestinationViewController1 {
self.delegate = vc.self
}
} else if segue.identifier == "SegueNameForDestinationViewController2" {
if let vc = segue.destinationViewController as? YourDestinationViewController2 {
self.delegate = vc.self
}
}
}
func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
if self.selectedItem == item {
self.delegate?.refresh()
}
self.selectedItem = item
}
}
In each tab view controller,
class TabViewController: UIViewController, ParentDelegate {
func refresh() {
//write your code here
}
}
iphone app - detect which tab bar item was pressed
How to get title of UITabBarItem in the More section?
- (void)tabBarController:(UITabBarController *)tabBarController
didSelectViewController:(UIViewController *)viewController
{
NSLog(@"controller class: %@", NSStringFromClass([viewController class]));
NSLog(@"controller title: %@", viewController.title);
if (viewController == tabBarController.moreNavigationController)
{
tabBarController.moreNavigationController.delegate = self;
}
}
Detect when UITabBar item selected swift
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.
How to detect when pressing down on UIBarButtonItem?
The general idea is, you need to create your own custom UIView
, and then pass that into this initialiser.
let customView = MyCustomBarButtonItem()
let barButtonItem = UIBarButtonItem(customView: customView)
As for how you implement the custom view so that you can detect touch downs, you have many choices.
You can either use touchesBegan
to detect the touch down, and touchesEnded
to detect a "tap" on the bar button item.
class MyCustomBarButtonItem: UIView {
// ...
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
// ...
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
// ...
}
}
Another way is to subclass UIButton
, and add target/action pairs for the .touchDown
/.touchUpInside
control events.
Related Topics
What Does an Exclamation Mark Mean in the Swift Language
Saving Custom Swift Class With Nscoding to Userdefaults
The Use of Swift 3 @Objc Inference in Swift 4 Mode Is Deprecated
Swift Constants: Struct or Enum
Why Optional Constant Does Not Automatically Have a Default Value of Nil
When Are Argument Labels Required in Swift
What Is a Good Example to Differentiate Between Fileprivate and Private in Swift3
Removeobjectsatindexes For Swift Arrays
How to Use Assets Catalog Color Sets
Changing Navigation Title Programmatically
Updating the Ui Using Dispatch_Async in Swift
Swift: Converting a String into a Variable Name
Iterate Over Object Class Attributes in Swift
Any Reason Not Use Use a Singleton "Variable" in Swift
Nsobject Subclass in Swift: Hash VS Hashvalue, Isequal VS ==