标签栏显示警报消息 [英] Tab bar shows alert message

查看:27
本文介绍了标签栏显示警报消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序有四个选项卡,其中 3 个移动到单独的视图控制器.第四个使用户能够从应用程序注销,我在这里需要的是在按下第四个选项卡时添加一条警报消息,有人可以帮助我吗?

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

推荐答案

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

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 >

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

then go to AppDelegeate.m and define a global boolean variable

bool shouldSelectViewController;

此变量决定是否应查看您的登录屏幕...虽然我们不会在代码中操作此变量,但由于某种原因,我的代码在不添加此变量的情况下无法按您的意愿工作它没有任何意义,但 xcode 让我感到惊讶!

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!

现在添加这个方法

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

        }

    }

这篇关于标签栏显示警报消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆