Ios 选项卡栏显示警报消息

Ios 选项卡栏显示警报消息,ios,uitabbar,Ios,Uitabbar,我的应用程序有四个选项卡,其中三个移动到单独的视图控制器。第四个是允许用户从应用程序中注销。我需要在按下第四个选项卡时添加一条警告消息。有人能帮我怎么做吗 我确实在寻找解决办法,但什么也没找到。 提前感谢在第四个视图中,控制器视图将出现方法显示警报视图。显示警报时,警报将弹出 -(void)viewWillAppear:(BOOL)animated { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert"

我的应用程序有四个选项卡,其中三个移动到单独的视图控制器。第四个是允许用户从应用程序中注销。我需要在按下第四个选项卡时添加一条警告消息。有人能帮我怎么做吗

我确实在寻找解决办法,但什么也没找到。
提前感谢

在第四个视图中,控制器
视图将出现
方法显示警报视图。显示警报时,警报将弹出

-(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
方法中,将出现
视图
使用
UIAlertView

使用UIAbbarController删除方法:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController  {
    if (tabBarController.selectedIndex == 3) {
        //show alert here
    }
这可能会有帮助 单击选项卡栏时,将调用此方法,因此请在此方法中处理您的逻辑

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


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

}

因此,如果您想在用户点击选项卡栏项目时向用户显示警报视图,并且不想更改为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;
}
当用户从警报视图中选择所需按钮时,默认情况下,
shouldShowAlert
将为
YES
,您可以执行以下操作:

shouldShowAlert = NO;
self.selectedIndex = indexOfYourAlertTabBarItem;

另外,请确保设置alertView的委托。

首先确保appdelegate.h文件包含UITabBar控制器delegate,以便能够访问UITabBar方法。。因此,AppDelegate.h的定义应该如下所示

@interface AppDelegate : UIResponder <UIApplicationDelegate,UITabBarControllerDelegate >
此变量决定是否应查看登录屏幕。。虽然我们不打算在代码中操作这个变量,但是由于某种原因,如果不添加这个变量,我的代码就不能像您希望的那样工作,这没有任何意义,但是xcode让我感到惊讶

现在添加此方法

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


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

        }

    }

添加
UIAlertView
您的
视图现在显示什么?@BuntyMadan我需要在第四个选项卡按下时显示此列表如何链接此选项卡?@user2323070您能告诉我在没有ViewController的情况下如何添加第四个选项卡吗@Prasadevadiga不抱歉,我错了,它有一个视图控制器,它是我电脑的主屏幕app@user2323070然后仅在主屏幕中显示警报,in view willbeen:method在第四个选项卡中没有视图控制器我只需要在按下时显示消息不可能没有视图控制器的选项卡Dear它是一个注销选项卡我需要一条消息来确认用户是否确定要注销这意味着消息将显示在当前的视图控制器的注销选项卡中按下?放一个虚拟vc并写入itI需要在当前视图控制器中显示警告选项卡被按下吗?不移动到单独的视图控制器,当用户单击“确定”时,他将移动到视图的主屏幕app@user2323070然后将其移动到我更新的第一个tabBarViewController,他也需要一个有效的viewcontroller@LithuT.V你能再解释一下吗。对不起,我对Xcoding是新手