As others have said, the general idea is to have a property on FirstViewController that stores the URL it needs to load, and then load that URL into the UIWebView when the view appears.
Here's an example of what it might look like, starting with the header:
@interface FirstViewController : UIViewController {
UIWebView *webView;
NSURL *storyURL;
}
@property (nonatomic, retain) IBOutlet UIWebView *webView; // this is set up in your nib
@property (nonatomic, retain) NSURL *storyURL;
@end
Now for the implementation:
@implementation FirstViewController
@synthesize webView;
@synthesize storyURL;
- (void)dealloc;
{
[storyURL release]; // don't forget to release this
[super dealloc];
}
- (void)viewDidAppear:(BOOL)animated;
{
// when the view appears, create a URL request from the storyURL
// and load it into the web view
NSURLRequest *request = [NSURLRequest requestWithURL:self.storyURL];
[self.webView loadRequest:request];
}
- (void)viewWillDisappear:(BOOL)animated;
{
// there is no point in continuing with the request load
// if somebody leaves this screen before its finished
[self.webView stopLoading];
}
@end
So now all you need to do in the controller for the previous view is get the story URL, pass it on to FirstViewController and push it.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *story = [stories objectAtIndex:[indexPath row]];
NSURL *storyURL = [NSURL URLWithString:[story objectForKey:@"link"]];
FirstViewController *firstViewController = [[FirstViewController alloc] initWithNibName:@"FirstView" bundle:[NSBundle mainBundle]]; // I'm pretty sure you don't need to pass in the main bundle here
firstViewController.storyURL = storyURL;
[self.navigationController pushViewController:firstViewController animated:YES];
[firstViewController release]; // don't leak memory
}
And that's about it. A few other points:
I assume the "link" value in your dictionary is already a string - the construction of a brand new string in your original example was unnecessary if this is the case. As you can see in my example, we can use this string directly to create the NSURL
instance.
In your original code, when you alloc/init your FirstViewController, you pass this straight into pushViewController
. This creates a memory leak as once UINavigationController
is done with it (after it gets popped off the navigation stack), it's retain count will still be 1. At the least you should call autorelease
but the most efficient thing to do here is simply alloc/init it, store it in a temp variable, then call release
straight after we've pushed it. This ensures that once it gets popped off the navigation stack, it will get deallocated.