views:

57

answers:

2

Hi,

I have an RSS parser, which downloads a link to each item from an RSS feed. How do I make it so that when a user selects a row in the UITable, it opens my web view (made following this tutorial http://dblog.com.au/iphone-development/iphone-sdk-tutorial-build-your-very-own-web-browser/) with the URL of the item (which can be gathered by news.link)

Thanks.

DidSelectRow code:

webView = [incidents objectAtIndex:indexPath.row];
    NSURLRequest *request = [NSURLRequest requestWithURL: news.link];
    [webView loadRequest:request];
    [self.navigationController pushViewController:webView animated:YES];
    [tableView deselectRowAtIndexPath:indexPath animated:YES]
    [webView release];
A: 

You need to implement tableView:didSelectRowAtIndexPath: in the view controller that controls your UITableView.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSURL* url = ... determine the URL to show ... ;

    ... load url in your Web view ... ;

    [tableView deselectRowAtIndexPath:indexPath animated:YES]; // Conform to Apple human-interface guidelines (Table View Programming Guide for iPhone OS)
}
Shaggy Frog
Thanks - it's that ... load url in your Web view ... ; part that I'm having trouble with. What should the code look like that goes there?
Graeme
Create a `UIWebView` and use its `loadRequest:` method.
Shaggy Frog
So what would the code look like? I've added above what mine is at the moment, but it's not working.
Graeme
You'll have to be more specific than "it's not working". Describe precisely what you're doing, what you are expecting to happen, and what happens instead.
Shaggy Frog
Basically I want a view which I have created called, WebView, to appear, showing the news.link. It builds, but I get a warning message saying "[webView loadRequest:request]; - the WebView may not respond to -LoadRequest" and then when I select a row, the app crashes.
Graeme
saying 2010-07-18 10:02:31.995 Auslection 2010[14239:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[News loadRequest:]: unrecognized selector sent to instance 0x5d4cdb0'
Graeme
You have to carefully consider what those error messages are telling you. In the first comment, it looks like you definitely have a typo -- `LoadRequest:` instead of `loadRequest:`. You should treat each compiler warning as if it were an error, and aspire to have 0 warnings during compilation.
Shaggy Frog
Also, can you explain the line "`webView = [incidents objectAtIndex:indexPath.row];`", specifically, if `incidents` is an `NSArray`/`NSMutableArray`, and if so, why you are storing `UIWebView` objects in it?
Shaggy Frog
Should I have an action for loadRequest in my WebView view controllers? As for the line above - incidents is an NSMutableArray - and that's where the news.link is coming from. Should I not have that line? Changing loadRequest to LoadRequest does nothing either.
Graeme
A: 

What is webView in your code sample?
Which class is it?
UIViewController or UIWebView?

There is a line where you are using it as view controller and there is a line where you try to use it as a web view.

I bet it's a view controller, because you write that you have a warning on loadRequest line...

Anyhow, your view controller that has the web view should have some method that will receive a URL request and will load it to the web view:

// In webViewController.h:
- (void)loadWebViewWithRequest:(NSURLRequest *)request;

// In webViewController.m:
- (void)loadWebViewWithRequest:(NSURLRequest *)request {
    [webView loadRequest:request];
}

This is the method that you will have to use in your didSelectRow method instead of the loadRequest...

Michael Kessler
Thanks. That seemed to do the job. Question though - you say I should be using a UIWebView - does that provide better support than a UIViewController?
Graeme
Newbie question - but how do I connect the above (void) request to the WebView?
Graeme
UIWebView is a view - the one that is responsible to show HTML on the screen. UIViewController is a background class that supplies the functionality to the view that is displayed on the screen (load it, react to user's actions etc.). In your case you have to implement a UIViewController (that you have already done) and display a UIWebView from that view controller or from Interface Builder by connecting it to the view controller both in Interface Builder and in the code. There are plenty of tutorials and code samples that learn you to use view controllers. Go over couple of these...
Michael Kessler