我正在使用UIView
向用户显示忙/处理线索,加载视图中的组件未响应用户触摸。让我详细说明我的问题。
我有大约3 UIViewController
秒,并根据情况显示来自View
的一个VieControllers
。我使用以下代码来更改当前视图
UIView *currentView = self.view;
UIView *theWindow = [currentView superview];
//Some other code
[currentView removeFromSuperview];
[self.view setFrame: [tileCountViewController.view bounds]];
[theWindow addSubview:tileCountViewController.view];
[theWindow setFrame:[tileCountViewController.view bounds]];
[theWindow setFrame: [[UIScreen mainScreen] bounds]];
然后我有一个加载视图,通常用于向用户显示忙碌/处理UI的类型。我从nib文件
以下列方式创建了加载视图- (id)init {
UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
NSString *nibName = @"LoadingViewController";
if(UIInterfaceOrientationIsLandscape(orientation)) {
nibName = @"LoadingView-landscape";
}
else {
nibName = @"LoadingView";
}
UINib *nib = [UINib nibWithNibName:nibName bundle:nil];
UIView *portraitView = [[nib instantiateWithOwner:self options:nil] objectAtIndex:0];
self = [super initWithFrame:portraitView.frame];
[self addSubview:portraitView];
[self bringSubviewToFront:portraitView];
return self;
}
在app delegate中,我初始化加载视图并将其添加到主窗口
if(!loadingViewIndicator){
loadingViewIndicator = [[LoadingView alloc]init];
}
[[[UIApplication sharedApplication]keyWindow]addSubview:loadingViewIndicator];
loadingViewIndicator.hidden = YES;
每当我需要显示加载/忙碌视图时,我只需像这样加载它
[loadingViewIndicator performSelectorOnMainThread:@selector(startLoadingViewIndicator:) withObject:startMsg waitUntilDone:NO];
- (void)startLoadingViewIndicator:(NSString *)startMsg{
self.hidden = NO;
[self.superview bringSubviewToFront:self];
[self.activityIndicator startAnimating];
[self.loadingMessage setText:startMsg];
self.loadingProgressView.progress = 0.0f;
[self refreshPosition];
}
一切正常但加载视图中的组件没有响应触摸。什么是错误的线索?
我刚才意识到问题是由于线程问题造成的。当用户开始下载任何文件时,我会显示此子视图。我之前的代码是直接开始下载
urlconnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately: YES];
我现在使用后台线程从另一种方法调用此方法,如下所示
[self performSelectorInBackground:@selector(startDownload) withObject:nil];
然后我在下载中添加了runloop,如
urlconnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately: YES];
CFRunLoopRun();
这很好用,我的子视图也很敏感。这花了很多时间。问题是有明确的代码示例可以帮助在后台线程中运行下载,同时使UI响应。