Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
232 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

        }

    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...