I'm working on an app and I would like to use the tab bar controller styling, but without the tabs. I would like it so that when I click on a tab button a function is run, but I don't want it to jump to a new tab. Is this possible and if so how do I do it?
Yes it is possible.
Implement the UITabBarControllerDelegate protocol's tabBarController:shouldSelectViewController: method execute your code and return NO.
Example
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
// Do your stuff here.
return NO;
}
Related
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
}
I have a UITabBarController with five tab bar items, one of them is share button action (to share the itunes store link - tell a friend). My issue is when I click on this tab, the UIActivityViewController has displayed with blank controller, I need to show the share window (UIActivityViewController) to appear above the last tab bar view selected to avoid the blank view of this tab bar item.
You can implement the UITabBarControllerDelegate protocol and use the
- tabBarController:shouldSelectViewController: method to intercept the tap on the share tab like this:
- (BOOL) tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
if (viewController == [tabBarController.viewControllers objectAtIndex:/*share button index*/]){
//show UIActivityViewController
return NO;
}
return YES;
}
I have a UITabViewController. I'm trying to modally present a UIImagePickerController when a specific tab bar item is selected. It must appear over the current UIViewController. This is like what happens in the Instagram or Periscope app when you press on the camera icon. However I can't figure out how to display the UIImagePickerController without displaying the UIViewController that's connected to the Tab Bar Item. Is this possible?
Thanks,
Yusuf
You can use the shouldSelectViewController method of UITabBarControllerDelegate to accomplish something like this.
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
// Check if it's the "image picker" tab
if (viewController == self.imagePickerTabView) {
// Present image picker
return NO;
}
// All other cases switch to the tab
return YES;
}
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
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
}