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
}
Related
I would like to prevent users from going to certain view controllers accessible through tab bar bar buttons and instead ask them to log in with an alert. I can disable the buttons, however, the only way to detect a button press on a disabled tab bar button (in order to fire the alert) seems to be to embed the button in another view with a gesture recognizer which seems complicated.
Alternatively, if I leave the buttons enabled, upon press they launch the view controller to which they are assigned with no way to launch the alert.
Is there a way to detect the press and pre-empt the segue to the view controller so I can launch an alert?
I have tried using the following method in my app delegate and in viewdidload of individual view controllers (that subscribe to uitabbarcontroller delegate protocol) but it is not getting fired.
- (void)tabBarController:(UITabBarController *)theTabBarController didSelectViewController:(UIViewController *)viewController
{
NSUInteger indexOfTab = [theTabBarController.viewControllers indexOfObject:viewController];
NSLog(#"Tab index pressed = %lu", (unsigned long)indexOfTab);
//launch alert
//prevent segue to view controller
}
The method you have to implement in your UITabBarController is :
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
If user is logged you return YES, if he's not, show an alert and return NO. You can check for which item was pressed by comparing viewController with the one(s) you want user to be logged.
You don't have to implement this method in a controller that subscribes to UITabBarController delegate protocol BUT to implement it in a subclass of your UITabBarController.
My UITabBarController caching everything about before selected item, It's nice but I want to dismiss each segue presented by me When I have clicked displayed view's item, twice consecutive same page.
How and What I need to do to provide this case? Detailed answer for solution It would be great.
Use tabbarControler delegate method
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
if (alreadyPushed){
return false
}
return true
}
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
I have situation where I need to make sure that user has completed certain steps before they move to another tab inside UITabBarController. So if the user is in middle of something and taps on another tab, I would like to show a UIAlertView saying "you must complete blah blah blah before you go to another tab."
Is it possible to to check this condition and cancel moving to another view controller?
Sure you can. I suppose you have your tabbar controller in the AppDelegate class. If so, set the AppDelegate to be its delegate. Then implement the following method
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
// place all the checks here
EditingViewController *editingController = //link to controller where editing is being made.
if (editingController && editingController.isEditing) {
//UIAlertView
return NO;
}
return YES;
}
At a guess you could try catching the view on it's way out and changing the selected index on the tab bar controller to be the view you wish to keep them on:
- (void)viewWillDisappear:(BOOL)animated {
self.tabBarController.selectedIndex = 0;
}
You might find that's a bit jerky though depending on the order of events, a quick google has found that if you can make your view controller a UITabBarControllerDelegate then you can implement:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
Which would allow you to catch them earlier. You might find it simplest to implement this in your App Delegate and have it know (or check) if it should allow the change away.
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;
}