【问题标题】:UIActivityIndicatorView not displaying right on UIAlertViewUIActivityIndi​​catorView 未在 UIAlertView 上正确显示
【发布时间】:2012-07-10 07:33:07
【问题描述】:

我正在编写一个 iOS 应用程序,我必须显示一个带有微调器的 UIAlertView。 有时,当我尝试在警报中心添加微调器时会出现问题,通常是在此之前有另一个警报时(不完全是规则,但这就是我注意到它失败的原因)。

我通过延迟微调器的创建部分解决了这个错误。这是我的做法(alert 是 UIAlertView 的成员):

这段代码在 viewDidLoad 中:

alert = [[[UIAlertView alloc] initWithTitle:@"Loading..." message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles: nil] autorelease];

[alert show];

[self performSelector:@selector(addSpinner) withObject:nil afterDelay:0.5f];

这是我的addSpinner函数:

- (void) addSpinner{

    UIActivityIndicatorView *indicator2 = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

    indicator2.center = CGPointMake(alert.bounds.size.width / 2, alert.bounds.size.height - 50);

    [indicator2 startAnimating];
    [alert addSubview:indicator2];
    [indicator2 release];
}

这个解决方案每次都有效,但我真的不喜欢它的完成方式。尽管在警报弹出半秒后显示微调器并没有那么令人不安,但必须有更好的方法来处理这个问题。我不是iOS专家,也不是高级程序员,但我不能使用事件吗?像“当实际显示警报时,使用 addSpinner 函数”之类的东西?我不知道该怎么做,在网上找不到太多帮助...

希望有人可以帮助我!谢谢。

【问题讨论】:

    标签: iphone objective-c ios uialertview


    【解决方案1】:

    您可以改用https://github.com/jdg/MBProgressHUD

    对于警报中的微调器,您可以使用这些方法。

    -(void)startLoading
    {
        alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Please Wait..." delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
    
        UIActivityIndicatorView *progress= [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(125, 50, 30, 30)];
        progress.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
        [alert addSubview:progress];
        [progress startAnimating];
        [progress release];
        [alert show];
    }
    -(void)stopLoading
    {
        [alert dismissWithClickedButtonIndex:0 animated:YES];
        [alert release];
    }
    

    【讨论】:

    • 谢谢,它看起来很不错,我可以尝试使用它。我无法将您的答案标记为我的“已接受答案”,因为它并不能真正解决问题:)
    • 我已经添加了一些代码来解决您的问题,希望对您有所帮助。
    • 感谢您的代码。我可能对我的原始帖子不够清楚:起初,当我使用indicator2.center = CGPointMake(alert.bounds.size.width / 2, alert.bounds.size.height - 50); 设置微调器相对于警报的位置时,微调器被向上和向左移动。然后,我尝试了您在此处编写的解决方案(显然以不同的方式,这就是我一开始没有注意到它的原因),将微调器的位置设置为“绝对”CGRectMake(125, 50, 30, 30)。我也不是很喜欢这个解决方案,因为它在某些情况下可能会引起麻烦:s
    • 我在以前的项目中使用了带指示器的警报,但我认为 MBProgressHUD 更好用,它可以节省您的时间,并且没有此类问题。
    • 谢谢,我想我会同意的。
    【解决方案2】:

    基本上问题看起来是在显示 UIAlertView 之前添加 UIActivityIndi​​cator。尝试使用 UIAlertView Delegate 方法添加 Indicator,如下所示。

    //AlertView delegate methods
    - (void)didPresentAlertView:(UIAlertView *)alertView
    {
        UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    
        indicator.center = CGPointMake(alertView.bounds.size.width/2, alertView.bounds.size.height/3 * 2);
    
        [indicator startAnimating];
        [alertView addSubview:indicator];
    }
    

    【讨论】:

    • 感谢您迟到的答案,它可能会解决问题,但我不再面对它了,因为我使用了 MBProgressHUD。
    【解决方案3】:

    试试这个

    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
    megaAlert = [[[UIAlertView alloc] initWithTitle:@"Please wait..."
                                                             message:nil delegate:self cancelButtonTitle:nil
                                                   otherButtonTitles: nil] autorelease];
    [megaAlert show];
    indicator = [[UIActivityIndicatorView alloc]                                                      initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
                indicator.center = CGPointMake(megaAlert.bounds.size.width / 2,                                            megaAlert.bounds.size.height - 45);
    [indicator startAnimating];
    [megaAlert addSubview:indicator];
    [indicator release];
    

    要关闭指示器,请使用此代码

    [megaAlert dismissWithClickedButtonIndex:0 animated:YES];
    megaAlert = nil;
    [indicator stopAnimating];
    

    【讨论】:

    • 谢谢。这基本上是我最初所做的,没有解除警报......你认为它会影响 UIActivityIndi​​cator 的位置吗?几分钟后我会告诉你它是否有效。
    • 看来问题确实出现了,因为UIActivityIndi​​cator的位置是在UIAlertView显示之前设置的。我不认为解除警报会解决这个问题......
    【解决方案4】:

    还要记住在主线程中关闭警报:

    dispatch_async(dispatch_get_main_queue(), ^{
        [self.alert dismissWithClickedButtonIndex:0 animated:YES];
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多