UITabBar: How to connect an item to a view controller? - ios

I am NOT using a UITabBarController.
I have a view controller and I added a UITabBar, it has two items.
How can I make sure that when the first item is clicked, it loads the content from a specific view controller, and when another item is clicked, loads the content from another view controller?

As mentioned in other answers (Detect when a tab bar item is pressed), You should implement the UITabBarDelegate protocol.
Once you're implementing this protocol, just use the
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item method to know when a user changes the selected item of a tab bar. implement this method, and you will have to check which item is selected, and manually change your view's content based on that.

You need to set the delegate of the UITabBar to be the UIViewController and the UIViewController needs to conform to the UITabBarDelegate protocol and implement the method
Swift
optional func tabBar(_ tabBar: UITabBar, didEndCustomizingItems items: [UITabBarItem], changed changed: Bool)
Objective-C
- (void)tabBar:(UITabBar *)tabBar didEndCustomizingItems:(NSArray<UITabBarItem *> *)items changed:(BOOL)changed
You can find details in the UITabBarDelegate documentation

Related

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

I am making an application with TabBar. In the Tab Bar there are 2 tabs. One of those tabs is history which is linked with a table view which shows history. I want to refresh that view every time when i click that so that i can get updated tableview. How can i do that? Thanks in advance.
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
Hi easiest solution for swift 4, to reload view by tapping again on tab bar icon.
#objc
func tabBarController(_ tabBarController: UITabBarController,
didSelect viewController: UIViewController) {
if tabBarController.selectedIndex == 0 {
viewController.viewDidLoad()
}
}

Custom action when clicking on UITabBarController

I have a Tab Bar Controller with four navigation controllers added to it. The navigation controllers appear as Tab Bar Items in the Tab Bar Controller. Now I want to add a fifth button to the tab bar, that does not open another view, but triggers some custom code. I want to display an overlaying "share menu" when clicking that Tab Bar Item regardless on which of the four pages the user is. How can I do that?
I can suggest to add dummy UIViewController to the last index and handle UITabBarControllerDelegate
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
if ([viewController == ...your dummy view controller...]) {
//Your custom action
return NO;
}
return YES;
}
Krivoblotsky has given the right answer! I'd like to elaborate a little more for anyone who is confused because for the full implementation there are a couple more moving parts. Let's say you have the app below. As it is when you click the home or profile icon the respective view will display. Let's say instead of the profile view to display, you want to add your custom transition / behavior.
To do this:
1. Given ProfileViewController class, you want include the UITabBarControllerDelegate in your ProfileViewController
#interface ProfileViewController : ViewController <UITabBarControllerDelegate> #end
2. Access your tabBarcontroller's delegate and set this as yourself in your ProfileViewController.m's viewDidLoad
self.tabBarController.delegate = self;
Essentially what this does is say hey, you know the tabBarController's delegate? (The guy that handles events) I know a guy and let this guy (self) handle those events instead. Like in English, you DELEGATE work to other people (you are the delegating object). The thing that handles the work, is the DELEGATE.
3. Implement the custom needed behavior
-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:
if ([viewController isKindOfClass:[ProfileViewController class]]){
NSLog(#"It's a profile");
return NO };
};
else{ return YES; }
The NO return says, when ProfileViewController is selected, do not do default behavior and display it's view.
Excellent explanation of delegates
In Storyboard, add a UIVIewController and connect it to the tab button you want to perform your custom action.
Give that UIViewController a unique title. e.g. "for custom action". It really doesn't matter, as nobody will ever see that title. It is just for you to use in the code below to identify that tab was tapped.
Create the class below and assign it to your UITabBarController in Storyboard
class TabBarController: UITabBarController, UITabBarControllerDelegate {
override func viewDidLoad() {
delegate = self
}
func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool {
if viewController.title == "for custom action" {
//do your custom actions
return false
}
return true
}
}
You should simply implement the following UITabBarDelegate method:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item;

Call delegate method on item select

I have 3 viewcontroller in tabbar. I am loading tabbar from appdelegate. Now I want to make an event when user change viewcontroller from tabbar. For that I have wrote delegate method didselectitem in appdelegate and set delegate in my viewcontroller. But delegate method is not called.
In my viewcontroller I am writing this:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
NSLog(#"tabbar is %#",tabBar.selectedItem);
}
in viewdidload:
MyAppDelegate.tabBar.delegate=self;
Make sure to set the delegate of the UITabBarController and not the tab bar itself. Also, make sure to implement the correct delegate method: tabBarController:didSelectViewController:
More information in the UITabBarControllerDelegate documentation.

UITabbarController delegate not calling?

Hi I am developing a UITabBar application.
I need:
1.When I select a TabBAr item I need to get the current selected index.
2.Based on the current index I want to perform some action in AppDelegate.
3.So is there a any delegate methods which called whenever I pressed the tab bar items.
4.Please suggest some way to check in AppDelegate every time I presses Tab Items
Thank You.
After a quick google:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
Make sure to link the delegate in IB...
Yes you can using this delegate method
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
NSLog(#"selected index : %i",[tabBarController selectedIndex]);
}
this method called whenever you touch any of TabBarItem.
First set the delegate Protocol e.g UITabBarControllerDelegate and then
put self.tabbarcontroller.delegate = self; in your ViewDidLoad or somewhere where the object get's initialized

Cant use TabBar delegate methods

I got an app with my custom TabBar Controller Class.
I tried to implement tabbar controller delegate method:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
NSLog(#"%i",tabBarController.selectedIndex);
}
But it doesnt work. Why?
in ViewDidLoad i write:
self.tabBarController.delegate = self;
And in .h i implement:
#interface BaseViewController : UITabBarController <UITabBarControllerDelegate>
In your custom TabBarController, do not use
self.tabBarController.delegate = self;
But use
self.delegate = self;
.tabBarController returns the nearest ancestor in the view controller hierarchy that is a tab bar controller, but your custom TabBarController IS the controller you want to target, so no need to search in its hierarchy
You have said, that it's your custom TabBarController. What is the customisation you've done? If you changed the TabBar panel and replaced it with your own to use
setSelectedIndex:
setSelectedViewController:
methods manually, then you should call delegate's methods manually too.
According to the Apple's documentation:
There are two types of user-initiated changes that can occur on a tab
bar:
The user can select a tab.
The user can rearrange the tabs.
Both types
of changes are reported to the tab bar controller’s delegate, which is
an object that conforms to the UITabBarControllerDelegate protocol.
Also check the UITabBarControllerDelegate Protocol Reference
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.
Delegate will respond only if user interacts with its UITabBar control.

Resources