在我的应用程序中,我在其中一个viewControllers中有一个表。当点击其中一行时,它会将用户带到不同的view/viewController,它就会正常工作。在这个新的viewController中,数据从后台的php脚本中解析出来。
这大约需要7-10秒,在这段时间内,我希望用户立即看到一个微调器,上面写着“正在加载..”。我已经实现了这一点,但是微调直到4-5秒后才开始加载。在此期间,屏幕完全冻结,在显示微调器/数据之前,我无法点击任何内容或返回。
我尝试将以下代码放在执行实际数据获取的方法中,也放在(不是同时) viewDidAppear和ViewDidLoad方法中,但是发生了相同的事情。
如果有人知道如何解决这个问题,我将不胜感激。
谢谢
[web loadRequest: [NSURLRequest requestWithURL: [NSURL URLWithString:@"http://xxx.xx.xx.xxx/stuff/xxx.php"]]];
[web addSubview:spinner];
timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector: @selector(tick) userInfo:nil repeats:YES];
load_message = [[UIAlertView alloc] initWithTitle:@"Loading..." message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
spinner= [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
spinner.center = CGPointMake(135.0, 60.0);
[load_message addSubview:spinner];
-(void) tick
{
if(!web.loading)
{
[spinner stopAnimating];
[load_message dismissWithClickedButtonIndex:0 animated:TRUE];
}
else {
[load_message show];
[spinner startAnimating];
}
}发布于 2012-01-05 12:22:47
您可以启动您的微调工具,然后在延迟后启动您的长进程:
- (void)someMethod
{
[spinner startAnimating];
[self performSelector@selector(doLongProcess:) withObject:someObject afterDelay:0.0];
}
- (void)doLongProcess:(id)someObject
{
//Some really long process
}您的微调很可能被阻塞,因为长进程可能在同一线程上发生。
发布于 2012-01-05 12:20:17
您必须执行以下操作之一(假设您将web请求放在名为‘startWebRequest’的方法中):
self performSelectorOnMainThread:@selector(startWebRequest) waitUntilDone:NO;
在开始web请求之前,self performSelectorInBackground:@selector(startWebRequest) withObject:nil;
NSTimer计划scheduledTimerWithTimeInterval:0.1f目标:自选择器:@选择器(StartWebRequest)用户信息:nil重复数:否;
https://stackoverflow.com/questions/8737527
复制相似问题