标签栏显示警报消息

时间:2013-05-17 11:34:46

标签: ios uitabbar

我的应用有四个标签,其中3个移动到单独的视图控制器。第四个使用户能够从应用程序注销我需要的是在第四个选项卡被按下时添加警报消息任何人都可以帮我怎么做?

我确实搜索了解决方案但没有找到任何结果。  提前致谢

6 个答案:

答案 0 :(得分:1)

首先确保你的appdelegate.h文件包含UITabBarControllerDelegate,以便能够访问UITabBar方法..所以你的AppDelegate.h的定义应该是这样的

@interface AppDelegate : UIResponder <UIApplicationDelegate,UITabBarControllerDelegate >

然后转到AppDelegeate.m并定义一个全局布尔变量

bool shouldSelectViewController;

此变量决定是否应该查看您的登录屏幕..虽然我们不打算在代码中操作此变量但由于某些原因我的代码无法正常工作而不添加此变量它没有任何意义,但xcode让我感到惊讶!

现在添加此方法

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

}

然后添加一个方法,如果用户确认警报

,则将用户转换为登录页面
   -(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;
                                }
                            }];

        }

    }

答案 1 :(得分:0)

在第四个viewController ViewWillappear方法内部显示alertview。当显示警告弹出时

答案 2 :(得分:0)

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

使用ForthViewController方法ViewWillappear使用UIAlertView

答案 3 :(得分:0)

使用UITabBarControllerDelegate方法:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController  {
    if (tabBarController.selectedIndex == 3) {
        //show alert here
    }

答案 4 :(得分:0)

它可能会有所帮助 当您单击tabbar时,将调用此方法,以便在此方法中处理您的逻辑

- (void)tabBarController:(UITabBarController *)rootController didSelectViewController:(UIViewController *)viewController {


}

答案 5 :(得分:0)

因此,如果您想要在用户点击标签栏项目时向用户显示警报视图,并且您不想更改为UITabBarItem的相应视图控制器,那么您应该尝试这样:

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    if([viewController isKindOfClass:[TheViewControllerOfYourAlertTab class]) {
       //TODO show alert here
       return NO;
    }
    return YES;
}

此外,如果要显示UITabBar的ViewController,它将在用户从警报视图中选择按钮后显示警报视图,则需要在if语句中添加额外的BOOL值类似的东西:

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    if([viewController isKindOfClass:[TheViewControllerOfYourAlertTab class] && shouldShowAlert) {
       //TODO show alert here
       return NO;
    }
    return YES;
}

默认情况下shouldShowAlertYES,当用户从警报视图中选择所需按钮时,您可以执行此操作:

shouldShowAlert = NO;
self.selectedIndex = indexOfYourAlertTabBarItem;

还要确保设置alertView的委托。