【问题标题】:Prevent changing Tabbar viewcontroller with UIAlertView防止使用 UIAlertView 更改 Tabbar 视图控制器
【发布时间】:2011-06-22 19:16:33
【问题描述】:

我试图让当用户按下标签栏项目时 UIAlertView 被调用,询问是否真的想要更改实际标签,问题是 UIAlertView 在得到答案之前不会停止代码,代码继续运行,并根据之前的值更改视图控制器,而不是实际的。

我试图等待一段时间的答案,但屏幕只会变暗并且没有弹出警报。我也看了这篇帖子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];}

【问题讨论】:

  • 老兄,你在这个问题上打败了我......谢谢!

标签: iphone uialertview uitabbar


【解决方案1】:
- (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 毫无意义。

【讨论】:

    猜你喜欢
    • 2022-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多