Prevent UINavigationController from popping on double tap of UITabBarController - ios

I have a UITabBarController in the root of my storyboard with three tabs. One of the tab is used to display the user information. I have set this tab to transition to the register/login page if the user has not registered. But the ViewController gets popped(and corrupts the Navigation Bar) when I double tap the tab icon. I cannot use the modal transition as it hides the TabBarController. What is the best solution to solve this issue?

You can use 'shouldSelectViewController' delegate of TabViewConroller to check if the selected viewController is same as the clicked one and skip it accordingly.
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
if(viewController == tabBarController.selectedViewController)
{
return NO;
}
}

Related

ios/objective-c: Detect tabbarbutton pressed event

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.

How can I click the tab bar to pop up a modal xcode

I want to click tab bar to pop up a modal view not just view controller.
(Like Instagram's camera tab bar to pup up camera view)
However when I drag from navigation controller to another view controller
I always got a black view when I run the app if I choose present modally.
So now, I can only choose root view controller.
Sorry for my poor English !
Can anyone understand and help me ?
The easiest way is to subclass uitabbarcontroller and use its delegate:
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
if ([viewController isKindOfClass:[SomeVC class]]) {
[self.navigationController presentViewController:vcToPresentModal animated:YES completion:NULL];
return NO;
}
return YES;
}
The main idea is to catch selection of some vc(it could be just empty UIViewController, connected with item, where should be modal presentation), cancel it,and show a modal vc.

share button as tab bar item on UITabBarController with no controller associated

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;
}

Add Login View before Tabbar Content ViewController

I have the following requirements:
A Tabbar Navigation, with 2 "Tabs" that include a Login. So if a user tab (for example) an Button3 of the UITabBar ill get a fullscreen login view - But only on 2 specific tabs.
This is the workflow:
If there is an active session (i am using a login session management) - i will not present a login button - but if there is none, a login window should appear in fullscreen.
So when ill now show in any of the RootViewControllers in ViewDidLoad the loginViewController, the "content" of the rootViewControllers still get loaded.
So ill my opinion ill should handle after click on a button, WITHOUT loading the RootViewController for that tab.
Can anyone help me whats the best approach to solve such things?
You can use -(BOOL)tabBarController:shouldSelectViewController: method of UITabBarDelegate to handle tab selection and present login view instead of activating tab if needed.
You'll possible need to store a callback (i. e. block), in which you activate tab manually using tabBar.selectedIndex = index.
-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
NSInteger index = [_tabBarController.viewControllers indexOfObject:viewController];
if (index==4)
{
if (_authorized)
{
return YES;
}
else
{
[self authorize];
__weak UITabBarController *tabBarController = _tabBarController;
_authorizationCallback = ^(){
//
tabBarController.selectedIndex = 4;
};
return NO;
}
}
return YES;
}

How to present UIImagePickerController from tab bar item

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;
}

Resources