0

我有一个视图,其中我从网络上获取数据 - 调用 php 脚本并获得 JSON 响应。我这样做的方式是执行 NSUrlRequest。

我这样做的方式是这样的:

-(void) viewDidLoad
{
..... 
    NSURLRequest *request = [NSURLRequest requestWithURL:
                             [NSURL URLWithString:url]];
    [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    NSLog(@"didReceiveResponse");
    [self.responseData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [self.responseData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"didFailWithError");
    NSLog(@"Connection failed: %@", [error description]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSLog(@"connectionDidFinishLoading");
    NSLog(@"Succeeded! Received %d bytes of data",[self.responseData length]);
...
}

现在我有另一个 UIView,它希望在该屏幕打开时显示 - 就像只针对这个屏幕(不是起始屏幕)的启动屏幕 - 这将是一个如何使用它的屏幕。例如,我只想显示 5 秒。

如果我将其[self openCustomView]放在 ViewDidLoad 内部,则会收到警告:

is not in the root view hierarchy 

并且视图没有打开。

如果我输入,connectionDidFinishLoading那么我正在等待数据下载,这可能需要一些时间。

有没有办法预览那个闪屏并在后台下载数据?

我的 openCustomView 是这样的:

 UIStoryboard *sb = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    CustomView *vc = [sb instantiateViewControllerWithIdentifier:@"CustomViewID"];
    vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentViewController:vc animated:YES completion:NULL];
4

1 回答 1

1

这有点猜测,但从消息中,我怀疑你是openCustomView从错误的位置打来的。我不相信self(及其视图)在加载过程中被视为层次结构的一部分。我会试试viewDidAppear的。

于 2013-11-02T13:09:10.753 回答