Check the needed view controller in delegate method tabBarController shouldSelectViewController: - ios

I have 3 TabBarItems in UITabBarController:
<UINavigationController: 0xc76a680>
<SplitViewController: 0xc76a170>
<UINavigationController: 0xca5e6f0>
And I have the method in AppDelegate:
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
NSLog(#"tab selected index %#",viewController);
if (viewController == nil ) // I NEED TO IMPLEMENT A CHECk HERE
{
//show popup
return NO; //does not change the tab
}
return YES; //does change the tab
}
So how to check that view controller which should be selected is the second Navigation Controller? Thx

try this code
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
BOOL result;
if (viewController == [self.tabBarController.viewControllers objectAtIndex:2]) //assuming the index of uinavigationcontroller is 2
{
NSLog(#"Write your code based on condition");
result = NO;
}
else {
result = YES;
}
return result;
}

You could check for viewcontroller.title property and then make the decision. Assuming that title for both viewcontrollers are different.

Related

UINavigationController: Clean interactive transition to view controller without navigation bar?

I have a view controller navigation hierarchy where only the top view controller hides has its navigation bar hidden. Normal pushing/popping is fine and works as expected. However, when the pop is interactive (drag-to-pop), the top opaque bar doesn't disappear until the transition is finished. I've implemented the logic in the following delegate call:
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
if( [viewController isKindOfClass:[MyTopViewController class]] == YES ) {
id<UIViewControllerTransitionCoordinator> transitionCoordinator = viewController.transitionCoordinator;
if( transitionCoordinator != nil && transitionCoordinator.initiallyInteractive == YES ) {
[transitionCoordinator notifyWhenInteractionEndsUsingBlock:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
if( context.isCancelled == YES ) {
return;
}
[navigationController setNavigationBarHidden:YES animated:animated];
}];
} else {
[navigationController setNavigationBarHidden:YES animated:animated];
}
} else {
[navigationController setNavigationBarHidden:NO animated:animated];
}
}
I've already attempted numerous variations of this, but each one has had various issues with interactive transitions, especially if the transition is canceled mid-way. What's the best way to approach this?

Double tapping the UITab to scroll to the top

I am having some trouble with tapping the selected tab to make the tableview go to the top. I am using a custom TabBarViewController and inside there I am using the following function:
- (void)tabBarController:(UITabBarController *)tabBarController
didSelectViewController:(UIViewController *)viewController
{
static UIViewController *previousController = nil;
if (previousController == viewController) {
// the same tab was tapped a second time
if ([viewController respondsToSelector:#selector(scrollToTop)]) {
[viewController scrollToTop];
}
}
previousController = viewController;
}
I am also getting the following Error:
No visible #interface for 'UIViewController' declares the selector 'scrollToTop'
I am wondering how I go about fixing this? If you guys need anymore information, please let me know. This has been bugging me for like a week!
Thanks all!!
Did your view controllers of tab bar controller is kind of UINavigationController?Maybe you should do like this:
- (void)tabBarController:(UITabBarController *)tabBarController
didSelectViewController:(UIViewController *)viewController
{
static UIViewController *previousController = nil;
if ([viewController isKindOfClass:[UINavigationController class]]) {
if (previousController == ((UINavigationController *)viewController).viewControllers.firstObject) {
// the same tab was tapped a second time
if ([viewController respondsToSelector:#selector(scrollToTop)]) {
[viewController scrollToTop];
}
}
previousController = ((UINavigationController *)viewController).viewControllers.firstObject;
} else {
previousController = viewController;
}
}

How to detect which child view controller was popped up in iOS?

I have view controller say A i am going from A to B.Then on B by pressing back button i am coming to A.Now i want to know from which view controller i am coming back.Please tell me how can i do this.I know i can do it by using viewWillAppear method but don't want to use this.
Please tell which is the best way of doing it?
-(void)viewWillAppear:(BOOL)animated
{
NSLog(#"view will appear called");
}
You have been push to B-VC from A-VC right.You can come back by popping.
self.navigationController!.popViewControllerAnimated(true)--Write this code in B-VC
VC-ViewController.
- (id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
animationControllerForOperation:(UINavigationControllerOperation)operation
fromViewController:(UIViewController *)fromVC
toViewController:(UIViewController *)toVC
{
NSLog(#"from VC class %#", [fromVC class]);
if ([fromVC isKindOfClass:[ControllerYouJustPopped class]])
{
NSLog(#"Returning from popped controller");
}
return nil;
}
This really saved me .
you can pass id of controller before pop A.h #property NSInteger childVC; A.m
-(void)viewWillAppear:(BOOL)animated
{
if (_childVC == 3){
//todo
}
}
B.m
-(void) viewWillDisappear:(BOOL)animated {
A *parent = (A *)self.navigationController.viewControllers[self.navigationController.viewControllers.count - 2];
parent.childVC = 3;
[super viewWillDisappear:animated];
}
I do not see other ways
You could use a navigation controller delegate and implement the following UINavigationControllerDelegate method:
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
UIViewController *fromViewController = [navigationController.transitionCoordinator viewControllerForKey:UITransitionContextFromViewControllerKey];
NSLog(#"%#", fromViewController.title);
}

Tab bar shows alert message

My app has four tabs 3 of them moves to a separate view controller . The fourth one enable the user to log out from the app what i need here is to add an alert message when the fourth tab is pressed can any one help me how to do so ?
I did search for solutions and found nothing .
Thanks in advance
first make sure your appdelegate.h file contains the UITabBarControllerDelegate to be able to access UITabBar methods .. so the definition of your AppDelegate.h should look like this
#interface AppDelegate : UIResponder <UIApplicationDelegate,UITabBarControllerDelegate >
then go to AppDelegeate.m and define a global boolean variable
bool shouldSelectViewController;
this variable decides whether or not your log in screen should be viewed or not .. although we are not going to manipulate this variable in the code but for some reason my code does not work as you wish without adding this variable it does not make any sense but xcode surprises me!
now add this method
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
if([viewController.title isEqualToString:#"your log in view controller title"]) {
//TODO show alert here
return shouldSelectViewController;
}
return YES;
}
}
then add a method to transform the user to the log in page if he confirms the alert
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 1) //here I assume that the alert has two buttons 0:cancel 1:Ok
{
// because you are in the AppDelegate you can't access the UITabBarController directly so you get it by writing this line of code
UITabBarController * tabBarController = (UITabBarController *)_window.rootViewController;
NSInteger destinationTabIdx = 3; //this is the index of the tab we want ot transfer the user to
UIView * fromView = tabBarController.selectedViewController.view; //move the user from current view he is in
UIView * toView = [[tabBarController.viewControllers objectAtIndex:destinationTabIdx] view]; // to this view which is view at tab index 3
//now do the transition to the view you want with optional animation
[UIView transitionFromView:fromView toView:toView duration:0.8
options:(destinationTabIdx > tabBarController.selectedIndex ? UIViewAnimationOptionTransitionFlipFromLeft: UIViewAnimationOptionTransitionFlipFromRight)
completion:^(BOOL finished) {
if (finished) {
tabBarController.selectedIndex = destinationTabIdx;
}
}];
}
}
Inside the forth viewController ViewWillappear method show the alertview.When ever it is shown the alert pops out
Use UITabBarControllerDelegate method:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
if (tabBarController.selectedIndex == 3) {
//show alert here
}
-(void)viewWillAppear:(BOOL)animated
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Alert" message:#"
msg" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
//Delegate
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex: (NSInteger)buttonIndex
{
if(buttonIndex==0)
{
self.tabBarController.selectedIndex = 1;
//Move User to the FirstTabBarViewController
}
}
In Your ForthViewController method ViewWillappear use UIAlertView
It may be helpful
When you click the tabbar this method will be called so handle your logic in this method
- (void)tabBarController:(UITabBarController *)rootController didSelectViewController:(UIViewController *)viewController {
}
So if you want to show to the user an alert view when he/she taps on a tab bar item and you don't want to change to the corresponding view controller of the UITabBarItem then you should try this:
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
if([viewController isKindOfClass:[TheViewControllerOfYourAlertTab class]) {
//TODO show alert here
return NO;
}
return YES;
}
Also, if you want to show the ViewController of the UITabBar which will show the alert view after the user selects a button from the alert view, you will need an extra BOOL value to be added in the if statement something like:
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
if([viewController isKindOfClass:[TheViewControllerOfYourAlertTab class] && shouldShowAlert) {
//TODO show alert here
return NO;
}
return YES;
}
And the shouldShowAlert by default will be YES, when the user selects the required button from the alert view you can do:
shouldShowAlert = NO;
self.selectedIndex = indexOfYourAlertTabBarItem;
Also make sure to set the delegate of your alertView.

Problem implementing UINavigationControllerDelegate

I may have some misunderstanding regarding the use of the UINavigationControllerDelegate protocol. Here is my situation:
I have a ViewController, let's call it, BViewController that may display a PopoverViewController. BViewController is the second ViewController in a NavigationContoller's stack, after AViewController. I need to dismiss the PopoverViewController when the user hits a button in BViewController and the app takes us back to the previous view--AViewController.
To do that, I have implemented the following in BViewController
- (void)viewWillDisappear:(BOOL)animated {
NSLog(#"BViewController will disappear");
// Check whether the popoverViewController is visible
if (self.popoverController.popoverVisible==YES) {
[self.popoverController dismissPopoverAnimated:NO];
}
}
However, that is not being called directly by the framework as BViewController is inside a NavigationController. Hence, I register a UINavigationControllerDelegate with my NavigationController and implement the following two methods:
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
// Pass the message on to the viewController in question
[viewController viewWillAppear:animated];
}
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
// Pass the message on to the viewController in question
[viewController viewWillDisappear:animated];
}
However, it seems that the passed in viewController parameter in both methods is the one that is about to be shown. I would have expected that the second method gives me access to the one that is about to disappear. So, when the user hits aforementioned button viewWillDisappear gets called on AViewController (which is about to be shown) and not on BViewController (which is about to disappear). Does that sound right? The apple documentation refers in both cases to
The view controller whose view and navigation item properties are being shown.
...which is not quite clear, I think. Thank you for some help, guys.
The two delegate method are both called for the same action (showing a view controller). The navigationController: willShowViewController:animated: is called before the new view controller is visible in the gui. The navigationController:navigationController didShowViewController:animated: is called after the new view controller is shown.
You will find this pattern in a lot of delegate protocols from apple. Unfortunately you do not have a delegate method in the NavigationViewController which tells you if the action was a pop or push.
I hook in my own protocol, which will know about the TO and FROM sides:
NavigationControllerDelegate.h:
#protocol NavigationControllerDelegate <NSObject>
#required
-(void) navigationController: (UINavigationController*) navController
willMoveFromViewController: (UIViewController*) from
toViewController: (UIViewController*) to;
#end
Instead of the regular UINavigationViewController, I then use a little helper class which keeps track of the view controllers:
NavigationHandler.h:
#interface NavigationHandler : NSObject <UINavigationControllerDelegate> {
NSMutableArray* m_viewControllers;
}
In my app delegate, I create one of these objects and set it as the delegate of the navigation controller:
...
m_navigationHandler = [[NavigationHandler alloc] init];
navigationController = [[UINavigationController alloc] initWithRootViewController: mainMenuViewController];
navigationController.delegate = m_navigationHandler;
...
And from then on its a simple case of comparing my own list of view controllers with what the navigation controller has:
NavigationHandler.m
#import "NavigationHandler.h"
#import "NavigationControllerDelegate.h"
#implementation NavigationHandler
-(id) init {
if ((self = [super init])) {
m_viewControllers = [[NSMutableArray alloc] init];
}
return self;
}
-(void) dealloc {
[m_viewControllers release];
[super dealloc];
}
- (void)navigationController:(UINavigationController *)navController
willShowViewController:(UIViewController *)viewController
animated:(BOOL)animated {
// Find out which viewControllers are disappearing and appearing
UIViewController* appearingViewController = nil;
UIViewController* disappearingViewController = nil;
if ([m_viewControllers count] < [navController.viewControllers count]) {
// pushing
if ([m_viewControllers count] > 0) {
disappearingViewController = [m_viewControllers lastObject];
}
appearingViewController = viewController;
[m_viewControllers addObject: viewController];
} else if ([m_viewControllers count] > [navController.viewControllers count]) {
// popping
disappearingViewController = [m_viewControllers lastObject];
appearingViewController = viewController;
[m_viewControllers removeLastObject];
} else {
return;
}
// Tell the view that will disappear
if (disappearingViewController != nil) {
if ([disappearingViewController conformsToProtocol: #protocol(NavigationControllerDelegate)]) {
if ([disappearingViewController respondsToSelector: #selector(navigationController:willMoveFromViewController:toViewController:)]) {
UIViewController<NavigationControllerDelegate>* vcDelegate = (UIViewController<NavigationControllerDelegate>*)disappearingViewController;
[vcDelegate navigationController: navController willMoveFromViewController: disappearingViewController toViewController: appearingViewController];
}
}
}
// Tell the view that will appear
if ([appearingViewController conformsToProtocol: #protocol(NavigationControllerDelegate)]) {
if ([appearingViewController respondsToSelector:#selector(navigationController:willMoveFromViewController:toViewController:)]) {
UIViewController<NavigationControllerDelegate>* vcDelegate = (UIViewController<NavigationControllerDelegate>*)appearingViewController;
[vcDelegate navigationController: navController willMoveFromViewController: disappearingViewController toViewController: appearingViewController];
}
}
}
#end

Resources