问题
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
回答1:
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;
}
}];
}
}
回答2:
Inside the forth viewController ViewWillappear
method show the alertview.When ever it is shown the alert pops out
回答3:
Use UITabBarControllerDelegate method:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
if (tabBarController.selectedIndex == 3) {
//show alert here
}
回答4:
-(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
回答5:
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 {
}
回答6:
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.
来源:https://stackoverflow.com/questions/16607988/tab-bar-shows-alert-message