I have a UITableViewController that needs to open a web view.
In my function I have:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
LinkViewController *linkViewController = [[LinkViewController alloc] initWithNibName:@"LinkViewController_iPhone" bundle:nil];
[linkViewController.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]]];
[self.navigationController pushViewController:linkViewController animated:YES];
}
I have connected all outlets and am not getting any warnings however I do not see the page pull up.
However if I go in the actual LinkViewController file and do something like:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *urlAddress = @"http://www.google.com";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[self.webView loadRequest:requestObj];
}
everything seems fine. I do not understand why ?
You should add a
URL
property to yourLinkViewController
. Then in your table view controller you do this:Now in your LinkViewController you do:
There are two problems with what you had originally:
webView
property before it was initialized. This is why nothing happened.LinkViewController
to the outside world. If you useLinkViewController
in many places in your app, you are requiring every user of the class to know to create a URL, create a request, and then tell the internal web view to start loading. All of that logic belongs inside theLinkViewController
. Now each user of the class only needs to know to set a simple URL property.