UITabbarController delegate not calling? - ios

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

Related

Xcode Storyboard Tabbed application (UITabBarController) - tell when tab selected

Using Xcode 6 I have created an Tabbed Application using a storyboard with the supplied template.
I need a function to fire when the third tab of the UITabBarController is selected.
I can’t use the ViewDidLoad as I need it fire every time the view is accessed by clicking on the tab (not just the first time( and I can't use ViewWillAppear as I need specific behaviour when view is accessed via the tab as opposed to segued back to from subsequent (modal) view controllers.
Any advice would be appreciated. Many thanks in advance.
Implement this delegate method of UITabBarControllerDelegate on some UIViewController class
- (void)tabBarController:(UITabBarController *)theTabBarController didSelectViewController:(UIViewController *)viewController {
NSUInteger indexOfTab = [theTabBarController.viewControllers indexOfObject:viewController];
// Your code here
}
OR
You can subclass UITabBarController and override the following method.
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
NSUInteger indexOfTab = [[theTabBar items] indexOfObject:item];
// Your code here}

want to call myMethod() when selecting same tab bar in IOS

I Want to call my own method, i.e. myMethod() when same tab bar is selected which is already selected.
I had tried to call didSelectMethod() but it is not called in my ViewController class.
Please help me in this,
Thank you
Jagveer Rana
Where you own your tabBarController instance there you set delegate
tabBarController.delegate= self; // It is must to set delegate
add the below delegate method
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
if ([viewController respondsToSelector:#selector(myMethod)]) {
[viewController performSelector:#selector(myMethod)];
}
}
It sounds like your ViewController class is not the delegate for your UITabBarController, otherwise tabBarController:didSelectViewController: would be called.
Make sure your delegate is linked up properly.
If that's not the problem, then there are a few other StackOverflow questions asking the same thing:
Tab bar, reload every time tab is pressed
Detect a re-tab on selected UITabbarItem

intercept UITabBar tap

I need to intercept a UITabBar tap precisely so I can conditionally abort moving to the intended ViewController (i.e. conditionally dismiss the tap).
I create my UITabBar in the storyboard. The UITabBar is the root view of my entire app. One of the tabBar items is a profile page (so to speak). If a user does not yet have an account, I want to ignore their clicking on that specific tabBar item. The behavior I seek is that the user will not leave the present ViewController they are in to go to the Tab selected. But instead, I want them to segue to the registration page. Any ideas how I might do this interception?
UPDATE
I have managed to add the following method in my AppDelegate. The method is called, but the transition is not intercepted. How do I intercept and abort the transition?
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
NSLog(#"Intercept tabBarController in app delegate tap gesture but then do nothing");
}
The NSLog line is printed, but transition still occurs.
Sounds like you are on the right path. But you should use
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
If you need more help, let me know.
You need to go with the UITabBar delegate of tabBar:didSelectItem:
Ex:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
// Use the item to do further
}

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.

How can I programmatically determine which tab button has been pressed in iOS?

I have a tabbar with 4 buttons on it. When the user presses the A button I want to call functionA. When the user presses the B button I want to call functionB and so on.
I have implemented the UITabBarDelegate.
I have this code and it fires as expected when any button on the tabbar is pressed.
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
What I am looking for is a code snippet that illustrates how to detect which button was pressed inside the delegate presumably using item.
Thanks to iwat's comment below I edited this to be simpler.
The following is a delegate call for the UITabBarController, rather than the UITabBar itself.
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
tabBarController.selectedIndex;
}

Resources