使用UIAlertView防止更改Tabbar视图控制器

时间:2011-06-22 10:46:46

标签: iphone uialertview uitabbar

我试图在用户按下标签栏项时调用UIAlertView,询问是否真的想要更改实际标签,问题是UIAlertView在获得答案之前不会停止代码,代码保持运行,并且取决于先前的值,是否更改了viewcontroller,而不是实际的。

我已经尝试了一会儿等待答案,但屏幕只会变暗并且警报没有弹出。我也读过这篇文章pause code execution until UIAlertview,我试过但是我无法让它发挥作用,有人可以帮助,谢谢!

- (BOOL) tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{

if (([self Myfunction]) && (viewController != [tabBarController.viewControllers objectAtIndex:0])){
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"text1" message:@"text2" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
    [alert show];
    [alert release];

    return boolean_var;
}

return YES;}

- (void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0) [self setBoolean_var:NO];
else [self setBoolean_var:YES];}

1 个答案:

答案 0 :(得分:3)

- (BOOL) tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    if ( !([self Myfunction]) || (viewController == [tabBarController.viewControllers objectAtIndex:0])) {
        return YES;
    }

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"text1" message:@"text2" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
    [alert show];
    [alert release];

    candidateViewController = viewController; // `candidateViewController` must be declared as an instance variable.

    return NO;
}

确定需要显示警报的视图控制器并将其保存在candidateViewController中并返回NO以延迟切换。根据警报视图的响应,您应该更改它。

- (void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex != 0)
        self.tabBarController.selectedViewController = candidateViewController;
}

最后一种方法假设很少。您的标签栏控制器由self.tabBarController引用,并且您正在设置boolean_var以将其返回到先前的方法。警告视图在该方法中是非阻塞的,因此使用boolean_var毫无意义。