2

我正在使用 aUIView向用户显示繁忙/处理线索,并且加载视图中的组件没有响应用户触摸。让我详细说明我的问题。我有大约 3UIViewController秒的时间,并根据情况显示其中之一ViewVieControllers我使用以下代码更改当前视图

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;

}

在应用程序委托中,我初始化加载视图并将其添加到主窗口

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 响应。

4

0 回答 0