【问题标题】:Weird UIAlertView with UIActivityIndicator带有 UIActivityIndi​​cator 的奇怪 UIAlertView
【发布时间】:2013-05-18 17:29:18
【问题描述】:

我刚刚问了这个问题Run code on background and get return code

我在执行后台任务时使用了带有活动指示器的 UIAlertView,最终代码:

    -(void)isConnectedToInternet:(void (^)(BOOL))block
{
    palert = [[UIAlertView alloc]initWithTitle:@"Comprobando conexión a internet" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
    [palert show];

    if(palert != nil) {
        UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

        indicator.center = CGPointMake(palert.bounds.size.width/2, palert.bounds.size.height-45);
        [indicator startAnimating];
        [palert addSubview:indicator];
    }

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:
                                    [NSURL URLWithString:@"http://www.google.com/"]];

    [request setHTTPMethod:@"HEAD"];

    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
        NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
        if (block) {
            [palert dismissWithClickedButtonIndex:0 animated:YES];
            block( ([httpResponse statusCode] == 200) ? YES : NO);
        }
    }];

它的代码与 UIAlertView 接受的答案相同,就在这样做之后,我显示了一个新的 UIViewController,它在 ViewDidLoad 上做类似的事情。

    alerta = [[UIAlertView alloc]initWithTitle:@"Descargando..." message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
[alerta show];

if(alerta != nil) {
    UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

    indicator.center = CGPointMake(alerta.bounds.size.width/2, alerta.bounds.size.height-45);
    [indicator startAnimating];
    [alerta addSubview:indicator];
}

很久以前,我使用相同的代码显示带有活动指示器的 UIAlertView,用于检查互联网连接的第一个 alertview 显示良好,但第二个没有...

如果两者的代码完全相同,我真的不明白为什么第一个在alerView 内显示指示器,第二个在alerView 外部显示它。

这只有在添加第一个 UIAlertView 之后才会发生,然后才添加它运行良好。

我真的被困在这个问题上,我找不到解决方案。

有人有什么想法吗?

编辑

简单的方法,我只是硬编码。

是的,alert.frame 似乎没有设置,可能类似于这个问题:Question

indicator.center = CGPointMake(140, 70);

【问题讨论】:

  • 只需在 alertview 图像中设置指示器视图框架
  • 第二次使用 alert 时,您的 CGRectZero 好像是 alerta.bounds。尝试继承 UIAlertView 并将活动指示器框架设置为 layoutSubviews
  • 嗨 @user2260916 检查这个 post 你会解决你的问题,祝你好运

标签: ios objective-c


【解决方案1】:

查看MBProgressHud

UIActivityIndicatorView and MBProgressHUD(@mobile.tutsplus.com)

还有

MBProgressHUD(@github.com)

也许对你有帮助。

【讨论】:

  • UIActivityIndi​​cator 可以满足我的需要,我不想使用第三方代码。
  • @Divz 应该有什么帮助?
  • 我认为你应该使用预定义的东西,这些东西可以在这个 MBProgressHud 类中轻松管理,同样的东西是你创建的东西所以这会节省你的时间。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-21
  • 1970-01-01
  • 1970-01-01
  • 2016-06-17
相关资源
最近更新 更多