objective-c modalViewController太快了

时间:2011-07-14 20:16:29

标签: objective-c syntax foreach language-features

我遇到了在某个边缘情况下解雇模态视图控制器的问题。当我检索要在UIWebView中显示的PDF时,我会显示模态视图。当我检索的文件非常小时,模态视图将尝试过早解除。我在包含UIWebView的视图控制器中呈现模态视图。我在UIWebView的didFinishLoad委托方法中忽略了它。

我没有动画模态视图的初始呈现......但是比我正在做的更安全吗?这还有可能失败,如果是这样,你会如何改变它?我一直在查看文档,到目前为止我所读过的内容并没有解决这种情况。

//
// This will download the file if not @ specific path, otherwise use local file.
// _myFileManager is a helper class and _myFileRecord is the backing data model
//
-(id)initWithNib... fileRecord:(MYFileRecord *)_myFileRecord
{
    [_myFileManager cacheFileAsync:_myFileRecord delegate:self];
}

- (void)viewDidLoad 
{
    // doesn't seem to work, NO for animated does seem to work
    [self.navigationController presentModalViewController:_splashController 
                                                 animated:YES];        
    _splashController.messageLabel.text = @"Retrieving File...";
}

- (void)recordSaved:(MyFileRecord *)myFileRecord fileName:(NSString *)fileName
{
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:fileName]];
    [_webView loadRequest:request];
}

- (void)webViewDidStartLoad:(UIWebView *)webView {
    _splashController.messageLabel.text = @"Opening File...";
}

//
// This fails when a small file is already cached to disk and the time
// for the webView to finishLoad is faster than the splashView can present itself
//
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [self.navigationController dismissModalViewControllerAnimated:YES];
}

3 个答案:

答案 0 :(得分:1)

您可以尝试测试以查看splashView是否已完成,并使用performSelector:afterDelay:稍后再回来查看。

我的想法是创建一个像这样的方法

-(void)dismissWhenReady {
    if ( splashView is finished) {
        [self.navigationController dismissModalViewControllerAnimated:YES];
    } else 
        [self performSelector:@selector(dismissWhenReady) afterDelay:1.0];
    }
}

答案 1 :(得分:1)

尝试在SplashController中实现viewDidAppear,以捕获视图完成动画的时间,并设置标记。那么你可以控制SplashController的视图是否已经使用这个标志加载完毕,并等待它还没有完成呢?

E.g。

-(void)viewDidAppear {
  if (shouldDismiss) {
    [self dismissViewControllerAnimated:YES];
  }
  readyToDismiss = YES;
}

在你的主要VC中:

-(void)webViewDidFinishLoading:(UIWebView*)webViewv
{
  if (_splashController.readyToDismiss) {
    [_splashController dismissViewControllerAnimated:YES];
  } else {
    _splashController.shouldDismiss = YES; // will dismiss in viewDidAppear
  }
}

答案 2 :(得分:0)

viewDidLoad过早触发(在显示之前),您将需要使用-(void)viewDidAppear:(BOOL)animated来显示您的模态视图以及一个标志,以了解它是否是第一次加载。如果它仍然显示不够长,则在所需的时间内添加延迟。