我有一個從Web服務加載數據的小iPhone應用程序。爲了確保在加載數據時沒有任何問題,我通過應用程序創建了一個半透明視圖,並使用CFRunloopRun()等待所有數據在後臺加載。這是該代碼:如何在後臺加載數據時使用UI事件
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事件。
謝謝 - 安迪
什麼視圖是按鈕? – MCannon 2011-03-19 01:57:23