How Can I handle an UITabBarItem clicked twice consecutive and dismiss segues? - ios

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
}

Related

How can I prevent an unwind segue from UITabBar?

I have a Show segue that is embedded in a UITabBarController. I want to prevent an unwind segue when I tap the currently selected tab unless a certain condition is met. I've tried using shouldPerformSegueWithIdentifier and canPerformUnwindSegueAction but neither appear to be triggered when unwinding in this way.
Not sure what you mean by unwind segue on a tab bar, but if you want to prevent a tab change, there is a delegate function on UITabBarController for that purpose.
Add the protocol to your tab bar class.
#interface YourTabbarViewController () <UITabBarControllerDelegate>
#end
Assign the delegate, then later implement the function.
#implementation YourTabbarViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.delegate = self;
}
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
if (preventTabChange)
return NO;
return YES;
}
UPDATE
OK, assuming you have set up relevant parts as on this picture, and you want to prevent the unwind from B to A if certain conditions are met. My solution as described above will work.
As you will get a query/notification whenever the Navigation Controller is about to become active, you could create your own sub-class of that to hold whatever information you need to decide if it should be allowed to show or unwind from a sub-view controller. In that case your prevention could look like this (expanding the shouldSelectViewController above):
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
if ([viewController isKindOfClass:[YourNavigationController class]]) {
if ([(YourNavigationController *)viewController preventUnwind])
return NO;
}
return YES;
}
Note that I purposely chose preventUnwind as a flag in your custom class to say what to do. This will default to NO when you move to the view controller, and thus allowing that.
Don't forget to set YourTabbarViewController as the class for the Tabbar View Controller and YourNavigationController as the Navigation Controller in the picture.

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
}

Prevent UINavigationController from popping on double tap of UITabBarController

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

Impose a condition needing to be satisfied before user changes to another tab

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.

Resources