UIActivityIndi​​catorView无法在UIAlertView上显示

时间:2012-07-10 07:33:07

标签: iphone objective-c ios uialertview

我正在编写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功能”这样的东西?我不知道怎么做,在网上找不到多少帮助...

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

4 个答案:

答案 0 :(得分:5)

您可以改为使用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];
}

答案 1 :(得分:4)

基本上问题似乎是在显示UIAlertView之前添加UIActivityIndi​​cator。尝试使用UIAlertView Delegate方法添加指标,如下所示。

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

答案 2 :(得分:2)

试试这个

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

答案 3 :(得分:1)

还记得在主线程中解除警报:

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