Question

I have a tableView with a list of items. When a selection is made, a new view controller is pushed on the tableView's navigationController viewController stack.

My question is this: one of the views in the view hierarchy associated with the pushed view controller requires the fetching of data from the network before it can render it's content. This fetching is done asynchronously via [NSURLRequest requestWithURL: ...]. What is the proper way to pass the data to the view that needs it and have that view call it's overloaded drawRect method? Currently, I do this:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {


    [_connection release];
    _connection = nil;

    [self performSelectorOnMainThread:@selector(GetDataFromCloudAndCreateView)
                        withObject:nil 
                     waitUntilDone:NO];


}

Method GetDataFromCloudAndCreateView instantiates the view form the data. Unfortunately, nothing is being rendered. I have tried [myView setNeedsDisplay] but it has no effect.

Could someone sketch the proper way to do this? Thanks.

Was it helpful?

Solution

What I've done in the past in a case like this is to create the new UIViewController in the connectionDidFinishLoading: method and then push it. Like so:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    [_connection release];
    _connection = nil;

    // Code to stop a loading indicator of some kind

    UIViewController *newView = [[SomeViewController alloc] initWithData:fetchedData];
    [self.navigationController pushViewController:newView animated:YES];
}

The initWithData: method on the new controller takes care of any instance variables it needs based on the data I passed to it and then when the view loads it will draw the view based on those variables.

Hope that answers your question.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top