Objective c 将UIAlertView置于UIActivityIndicator视图上方
我想马上在我的UIActivityIndicator视图上方查看我的UIAlert,我有以下代码:Objective c 将UIAlertView置于UIActivityIndicator视图上方,objective-c,view,uialertview,uiactivityindicatorview,Objective C,View,Uialertview,Uiactivityindicatorview,我想马上在我的UIActivityIndicator视图上方查看我的UIAlert,我有以下代码: - (void)showWithLabelDeterminate{ HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view]; [self.navigationController.view addSubview:HUD]; // Set determinate mode
- (void)showWithLabelDeterminate{
HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
[self.navigationController.view addSubview:HUD];
// Set determinate mode
HUD.mode = MBProgressHUDModeDeterminate;
HUD.delegate = self;
HUD.labelText = @"Loading";
// myProgressTask uses the HUD instance to update progress
[HUD showWhileExecuting:@selector(myProgressTask) onTarget:self withObject:nil animated:YES];
}
- (void)myProgressTask
{
float progress = 0.0f;
while (progress < 0.99f)
{
progress += 0.01f;
HUD.progress = progress;
usleep(50000);
if(progress > 0.02f & progress < 0.05f)
{
NSString * string = [NSString stringWithFormat:@"http://localhost:8080/......"];
NSURL *url = [NSURL URLWithString:string];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if(!urlData)
{
[self simpleAlert];
}
else
{
datos = [[NSMutableString alloc]initWithData:urlData encoding:NSISOLatin1StringEncoding];
}
}
}
}
-(void)simpleAlert
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Dont connect!!" delegate:self cancelButtonTitle:@"Refresh" otherButtonTitles:nil];
[alertView show];
[alertView release];
}
-(无效)用标签显示终止{
HUD=[[MBProgressHUD alloc]initWithView:self.navigationController.view];
[self.navigationController.view addSubview:HUD];
//设置确定模式
HUD.mode=MBPROGRESS HUD mode DEMINATE;
HUD.delegate=self;
HUD.labelText=@“加载”;
//myProgressTask使用HUD实例更新进度
[HUD showWhileExecuting:@selector(myProgressTask)onTarget:self with Object:nil动画:YES];
}
-(void)myProgressTask
{
浮动进度=0.0f;
同时(进度<0.99f)
{
进度+=0.01f;
HUD.progress=进度;
美国LEEP(50000);
如果(进度>0.02f&进度<0.05f)
{
NSString*string=[NSString stringWithFormat:@]http://localhost:8080/......"];
NSURL*url=[NSURL URLWithString:string];
NSMutableURLRequest*请求=[NSMutableUrlRequestRequestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&响应错误:&错误];
如果(!urlData)
{
[自我简化];
}
其他的
{
datos=[[NSMutableString alloc]initWithData:urlData编码:NSISOLATIN1STRINGCODING];
}
}
}
}
-(无效)simplelert
{
UIAlertView*alertView=[[UIAlertView alloc]initWithTitle:@“不连接!!”委托:自取消按钮:@“刷新”其他按钮:nil];
[警报视图显示];
[警报视图发布];
}
问题是我在UIActivityIndicatorView上方看不到我的UIAlertView…
myProgressTask
正在后台线程上执行,或者正在通过调用usleep
阻止主线程
如果它位于后台线程上,那么您使用的UIAlertView
是无效的,因为在一般情况下,UIKit只能从主线程使用。最快的解决方案是使用performselectornmainthread:…
调用simplelert
如果它在主线程上,那么您可能正在阻止UIKit运行。关于使用非阻塞的
NSURLConnection
和使用NSTimer
或performSelector:withObject:afterDelay:
来安排暂停后的事情,下面会有明显的评论。我想完成我的UIActivityIndicator视图,因为在这一刻它是不必要的。在中没有UIActivityIndicator视图发布的代码,大概是内置在MBProgressHUD中的?如果非要我猜的话,我会说,在我的两次猜测中,您可能想更改对SimpleAllert的调用,以便在主线程上发出它。是的,是的,抱歉,Tommy,我提到了MBProgressHUD,是的,我想关闭此progressHUD并初始化我的UIAlertView。解决方案:[HUD showWhileExecuting:@selector(SimpleAllert)onTarget:self with object:nil动画:YES];