0

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

我确实搜索了解决方案,但一无所获。提前致谢

4

6 回答 6

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

        }

    }
于 2013-05-20T16:50:37.120 回答
0

在第四个 viewControllerViewWillappear方法中显示警报视图。当它显示时,警报会弹出

于 2013-05-17T11:41:26.037 回答
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

于 2013-05-17T11:43:01.817 回答
0

使用 UITabBarControllerDelegate 方法:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController  {
    if (tabBarController.selectedIndex == 3) {
        //show alert here
    }
于 2013-05-17T11:56:52.393 回答
0

这可能会有所帮助当您单击标签栏时,将调用此方法,因此请在此方法中处理您的逻辑

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


}
于 2013-05-17T11:59:36.120 回答
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;
}

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

shouldShowAlert = NO;
self.selectedIndex = indexOfYourAlertTabBarItem;

还要确保设置你的 alertView 的委托。

于 2013-05-17T12:28:54.567 回答