如何在iOS中使用活动指示器显示警告框?

22

我想展示一个弹窗,里面显示"正在加载数据"的消息和旋转的活动指示器。我应该怎么做呢?

7个回答

27

注意:此解决方案不适用于iOS 7及以上版本。

以下是我的想法:

alertView = [[UIAlertView alloc] initWithTitle:@"Submitting Entry"
                                       message:@"\n"
                                      delegate:self
                             cancelButtonTitle:nil
                             otherButtonTitles:nil];

UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];   
spinner.center = CGPointMake(139.5, 75.5); // .5 so it doesn't blur
[alertView addSubview:spinner];
[spinner startAnimating];
[alertView show];

可以使用以下代码关闭窗口:

[alertView dismissWithClickedButtonIndex:0 animated:YES];

5
使用spinner.center = CGPointMake(0.0, -spinner.frame.size.height); 来将旋转器始终定位在警报框的底部中心;同时,使用spinner.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;,使其自适应调整顶部边距、左侧边距和右侧边距。 - MrTJ
旋转器不显示。我的alterView只显示“正在提交条目”。我正在使用iOS 7。 - coolcool1994
@coolcool1994 这还没有在iOS 7上进行测试。这是两年前的回答 ;) - jowie
没错 :) 我希望我能知道这个的iOS7方式 :} - coolcool1994

25

这在 iOS 7 上有效

在 iOS 7 及以上版本中,addSubView 在 UIAlertView 上不起作用。请尝试使用以下方法代替。

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Loading data" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];

UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[indicator startAnimating];

[alertView setValue:indicator forKey:@"accessoryView"];
[alertView show];

并予以解雇

[alertView dismissWithClickedButtonIndex:0 animated:YES];

这是在iOS 7上(针对此评论的当前日期)可行的解决方案。 - OhadM

20

您可以将标签和活动指示器作为警告视图的子视图添加。您需要像这样做:

myAlertView = [[UIAlertView alloc] initWithTitle:@"Loading" message:@"\n\n"
                                        delegate:self
                               cancelButtonTitle:@""
                               otherButtonTitles:@"OK", nil];  

UIActivityIndicatorView *loading = [[UIActivityIndicatorView alloc]
                initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];   
loading.frame=CGRectMake(150, 150, 16, 16);
[myAlertView addSubview:loading];
[myAlertView show];

在这种情况下最好使用UIActionSheet...


谢谢,我会使用UIActionSheet + UIActivityIndicatorView。 - Voloda2
1
为什么在这种情况下应该使用UIActionSheet呢?(我知道苹果可能会同意您的看法,但我想知道为什么)。您没有使用任何私有API,对吧? - Nate
3
你缺少了[loading startAnimating],同时还有空白按钮显示。 - jowie
你可能错过了 [loading startAnimating]; 我猜。 - dinesh

6

您可以将标签和活动指示器作为警报视图的子视图添加。您需要执行以下操作...

- (IBAction)showAlertWithActivity:(id)sender{

alerta = [[UIAlertView alloc] initWithTitle:@"Guardando datos..."
                                            message:@"\n"
                                           delegate:self
                                  cancelButtonTitle:nil
                                  otherButtonTitles:nil];

        UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        spinner.center = CGPointMake(139.5, 75.5); // .5 so it doesn't blur
        [alerta addSubview:spinner];
        [spinner startAnimating];
        [alerta show];


        [self performSelector:@selector(close) withObject:self afterDelay:1];


    }
    -(void)close{

        [alerta dismissWithClickedButtonIndex:0 animated:YES];

    }

1

在你的.h文件中添加以下内容:

UIAlertView *connectingAlert;

并在你的.m文件中添加以下两个函数:

//show loading activity.
- (void)startSpinner:(NSString *)message {
    //  Purchasing Spinner.
    if (!connectingAlert) {
        connectingAlert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(message,@"")
                                                 message:nil
                                                delegate:self
                                       cancelButtonTitle:nil
                                       otherButtonTitles:nil];
        connectingAlert.tag = (NSUInteger)300;
        [connectingAlert show];

        UIActivityIndicatorView *connectingIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        connectingIndicator.frame = CGRectMake(139.0f-18.0f,50.0f,37.0f,37.0f);
        [connectingAlert addSubview:connectingIndicator];
        [connectingIndicator startAnimating];

    }
}
//hide loading activity.
- (void)stopSpinner {
    if (connectingAlert) {
        [connectingAlert dismissWithClickedButtonIndex:0 animated:YES];
        connectingAlert = nil;
    }
    // [self performSelector:@selector(showBadNews:) withObject:error afterDelay:0.1];
}

然后调用
[self startSpinner:@"Your message........"];
[self stopSpinner];

1
在Swift 3中
let loadingAlertController = UIAlertController(title: "Loading", message: nil, preferredStyle: .alert)

let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .gray)
activityIndicator.translatesAutoresizingMaskIntoConstraints = false

loadingAlertController.view.addSubview(activityIndicator)

let xConstraint = NSLayoutConstraint(item: activityIndicator, attribute: .centerX, relatedBy: .equal, toItem: loadingAlertController.view, attribute: .centerX, multiplier: 1, constant: 0)

let yConstraint = NSLayoutConstraint(item: activityIndicator, attribute: .centerY, relatedBy: .equal, toItem: loadingAlertController.view, attribute: .centerY, multiplier: 1.4, constant: 0)

NSLayoutConstraint.activate([ xConstraint, yConstraint])

activityIndicator.isUserInteractionEnabled = false
activityIndicator.startAnimating()

let height: NSLayoutConstraint = NSLayoutConstraint(item: loadingAlertController.view, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 80)

loadingAlertController.view.addConstraint(height);

self.present(loadingAlertController, animated: true, completion: nil)

0
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alarm" message:@"" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];

UIActivityIndicatorView *loading = [[UIActivityIndicatorView alloc]
                                    initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];   
loading.frame=CGRectMake(125, 50, 36, 36);
[loading startAnimating];
[alert addSubview:loading];
[alert show];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接