如何使用活动指示器显示alertview?

Vol*_*da2 22 iphone uialertview uiactivityindicatorview ios

我想向alertview显示消息:"正在加载数据"和旋转活动指示器.我怎样才能做到这一点?

jow*_*wie 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];
Run Code Online (Sandbox Code Playgroud)

使用以下代码解雇代码:

[alertView dismissWithClickedButtonIndex:0 animated:YES];
Run Code Online (Sandbox Code Playgroud)

  • 使用`spinner.center = CGPointMake(0.0,-spinner.frame.size.height); spinner.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;`始终将微调器定位在警报的底部中心 (5认同)

And*_*per 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];
Run Code Online (Sandbox Code Playgroud)

并解雇它

[alertView dismissWithClickedButtonIndex:0 animated:YES];
Run Code Online (Sandbox Code Playgroud)


Sat*_*Sat 20

您可以添加标签和activityindicator作为警报视图的子视图.你必须做这样的事情

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];
Run Code Online (Sandbox Code Playgroud)

..更好地在这种情况下使用UIActionSheet ...

  • 你错过了`[loading startAnimating]`并且还有空按钮显示. (3认同)

osc*_*lon 6

您可以添加标签和activityindicator作为警报视图的子视图.你必须做这样的事......

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

    }
Run Code Online (Sandbox Code Playgroud)