keyWindow顶部的叠加视图快速消失(iOS)

时间:2011-09-14 12:40:02

标签: ios uiview overlay

启动时,我的应用会检查更新。如果它可用,我尝试使用此指令将叠加视图放在彼此的顶部:

[[[UIApplication sharedApplication] keyWindow] addSubview:overlay];

叠加视图出现但很快消失,而我想在更新过程结束时将其删除。相反,如果我将叠加视图添加为当前视图的子视图,叠加视图将保持在顶部,直到更新过程完成。

我必须将叠加视图放在keyWindows的顶部,因为我的应用有一个标签栏,所以如果我将叠加视图放在当前视图的顶部,如果用户点击其中的另一个项目,叠加视图将会消失标签栏。

可能是因为我在NSInvocation的单独任务中移动了检查更新的事实?这是我的相关代码:

[在viewDidLoad中]

    NSOperationQueue *queue = [NSOperationQueue new];

    NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(checkForUpdate) object:nil];

    [queue addOperation:operation];
    [operation release];

[在checkForUpdate中]

    if (![localVersion isEqualToString:remoteVersion])
    {
        [self performSelectorOnMainThread:@selector(doUpdate) withObject:nil waitUntilDone:NO];
    }

[在doUpdate中]

UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"Update" message:@"Download the latest version of...?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];

[alertView show];
[alertView release];

[in willDismissWithButtonIndex]

if (buttonIndex == 1)
{
    CGRect overlayFrame = [[UIApplication sharedApplication] keyWindow].bounds;
    overlay = [[UIView alloc] initWithFrame:overlayFrame];
    overlay.backgroundColor = [UIColor blackColor];
    overlay.alpha = 0.7;

    // Do other staff...

    [[[UIApplication sharedApplication] keyWindow] addSubview:overlay];

    NSURL *url = [NSURL URLWithString:@"http:www.testserver.com/test/UpdatePack.zip"];
    NSURLRequest *theRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:300.0];

    connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    connection = nil;
}

[在connectionDidFinishLoading中]

[overlay removeFromSuperview];

1 个答案:

答案 0 :(得分:3)

我解决了将问题添加到tabbarViewController视图的问题:

[self.tabBarController.view addSubview:overlay];