我有一个从 Web 服务加载数据的小 iPhone 应用程序。为了确保在加载数据时不会出错,我在应用程序上创建了一个半透明视图,并使用 CFRunloopRun() 等待所有数据在后台加载。这是代码:
self.connection = [[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];
// Now show an animation
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
UIView *window = [[UIApplication sharedApplication] keyWindow];
UIView *shield = [[UIView alloc] initWithFrame:window.bounds];
shield.backgroundColor = [UIColor blackColor];
shield.alpha = 0.5f;
[window addSubview:shield];
spinner.center = shield.center;
[shield addSubview:spinner];
spinner.hidden = NO;
NSLog( @"JCL.callServerWithRequest(), spinner view: %@, shield view: %@, window: %@", spinner, shield, window );
[spinner startAnimating];
// Hand over to the Runnloop to wait
CFRunLoopRun();
[spinner stopAnimating];
[spinner removeFromSuperview];
[spinner release];
[shield removeFromSuperview];
[shield release];
这工作正常,除了在加载后播放某个按钮的任何点击,因此如果用户点击下载按钮两次,他也会下载两次。
知道如何在屏蔽被移除之前使用 UI 事件。
谢谢 - 安迪