将视图覆盖添加到iPhone应用程序

将视图覆盖添加到iPhone应用程序,iphone,Iphone,我正在尝试这样做: - (void)sectionChanged:(id)sender { [self.view addSubview:loadingView]; // Something slow [loadingView removeFromSuperview]; } 其中loadingView是带有UIActivityIndicatorView的半透明视图。但是,添加的子视图更改似乎在该方法结束之前不会生效,因此在视图变为可见之前将其删除。如果删除removeFr

我正在尝试这样做:

- (void)sectionChanged:(id)sender {
    [self.view addSubview:loadingView];
    // Something slow
    [loadingView removeFromSuperview];
}

其中loadingView是带有UIActivityIndicatorView的半透明视图。但是,添加的子视图更改似乎在该方法结束之前不会生效,因此在视图变为可见之前将其删除。如果删除removeFromSuperview语句,则在完成缓慢的处理后,视图将正确显示,并且永远不会被删除。有没有办法解决这个问题?

在后台线程中运行缓慢的进程:

- (void)startBackgroundTask {

    [self.view addSubview:loadingView];
    [NSThread detachNewThreadSelector:@selector(backgroundTask) toTarget:self withObject:nil];

}

- (void)backgroundTask {

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    // do the background task

    [self performSelectorOnMainThread:@selector(backgroundTaskDone) withObject:nil waitUntilDone:NO];
    [pool release];

}

- (void)backgroundTaskDone {

    [loadingView removeFromSuperview];
}

在后台线程中运行缓慢的进程:

- (void)startBackgroundTask {

    [self.view addSubview:loadingView];
    [NSThread detachNewThreadSelector:@selector(backgroundTask) toTarget:self withObject:nil];

}

- (void)backgroundTask {

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    // do the background task

    [self performSelectorOnMainThread:@selector(backgroundTaskDone) withObject:nil waitUntilDone:NO];
    [pool release];

}

- (void)backgroundTaskDone {

    [loadingView removeFromSuperview];
}

两个潜在的问题浮现在脑海中,都围绕着如何实现“在这里做一些慢的事情”代码

首先,如果它锁定了主线程,那么应用程序的UI可能没有及时重新绘制以显示视图,即添加子视图、紧循环/密集处理占用主线程,然后在删除视图后立即重新绘制

其次,如果异步执行“缓慢的操作”,则在运行缓慢的处理时删除视图

可以肯定的是,您的要求如下:

  • 添加子视图以显示某种“加载”视图
  • 调用运行缓慢的功能
  • 慢运行功能完成后,删除“加载”子视图
  • - (void)beginProcessing {
        [self.view addSubview:loadingView];
        [NSThread detachNewThreadSelector:@selector(process) toTarget:self withObject:nil];
    }
    
    - (void)process {
    
        // Do all your processing here.
    
        [self performSelectorOnMainThread:@selector(processingComplete) withObject:nil waitUntilDone:NO];
    }
    
    - (void)processingComplete {
        [loadingView removeFromSuperview];
    }
    


    您还可以通过NSOperations实现类似的功能。

    脑海中浮现出两个潜在的问题,这两个问题都围绕着您如何实现“在这里做一些慢的事情”代码

    首先,如果它锁定了主线程,那么应用程序的UI可能没有及时重新绘制以显示视图,即添加子视图、紧循环/密集处理占用主线程,然后在删除视图后立即重新绘制

    其次,如果异步执行“缓慢的操作”,则在运行缓慢的处理时删除视图

    可以肯定的是,您的要求如下:

  • 添加子视图以显示某种“加载”视图
  • 调用运行缓慢的功能
  • 慢运行功能完成后,删除“加载”子视图
  • - (void)beginProcessing {
        [self.view addSubview:loadingView];
        [NSThread detachNewThreadSelector:@selector(process) toTarget:self withObject:nil];
    }
    
    - (void)process {
    
        // Do all your processing here.
    
        [self performSelectorOnMainThread:@selector(processingComplete) withObject:nil waitUntilDone:NO];
    }
    
    - (void)processingComplete {
        [loadingView removeFromSuperview];
    }
    


    您还可以通过NSOperations实现类似的功能。

    非常好,谢谢。dannywartnaby的答案几乎是一样的,但我会接受你的答案,因为你记住了自动释放池。太好了,谢谢。dannywartnaby的答案几乎是一样的,但我要让你的答案被接受,因为你记住了一个自动释放池。