【发布时间】: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