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

时间:2011-02-05 12:02:06

标签: ios iphone uialertview uiactivityindicatorview

我想显示带有消息的alertview:“正在加载数据”和旋转活动指示器。我怎么能这样做?

7 个答案:

答案 0 :(得分: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];

答案 1 :(得分:25)

适用于iOS 7

addSubView不适用于iOS 7及更高版本的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];

答案 2 :(得分: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];

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

答案 3 :(得分: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];

    }

答案 4 :(得分: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];

答案 5 :(得分: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)

答案 6 :(得分: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];