views:

426

answers:

1

I have an app, where there's one UIWebView and a UITableView. I don't want to create many .xib's, so I decided to make one .xib for all elements of the table. When user chooses a table element, the UIWebView appears and I want it to load data from different .html's depending on the name of the parent controller. The html's contain text and images (formulas converted to images).

I tried this:

if (selectedTableElement==@"FirstElement") {
    [childController.message loadRequest:[NSURLRequest requestWithURL:
        [NSURL fileURLWithPath:[[NSBundle mainBundle] 
        pathForResource:@"_" ofType:@"html"]isDirectory:NO]]];
}

And then

myWebView=message;

But it didn't work.

Maybe it's possible to display the same content (but not in .html) in UITextView?

Thanks in advance!

+1  A: 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch ([indexPath row])
    {
        case 0:
            [self loadFoo];
            break;
        case 1:
            [self loadBar];
            break;
    }

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)loadFoo
{
    [self loadFile:@"foo.html"];
}

- (void)loadBar
{
    [self loadFile:@"bar.html"];
}

- (void)loadFile:(NSString*)file
{
    NSString* resourcePath = [[NSBundle mainBundle] resourcePath];
    NSString* sourceFilePath = [resourcePath stringByAppendingPathComponent:file];
    NSURL* url = [NSURL fileURLWithPath:sourceFilePath isDirectory:NO];
    NSURLRequest* request = [NSURLRequest requestWithURL:url];
    [myWebView loadRequest:request];
}
Shaggy Frog
Thanks for the code, but I've faced one more problem-my WebView and my UITableViewController are implemented in different files. How can retrieve the WebView from another file?
Knodel
I think "retrieve" is the wrong word here. You *associate* a view controller with a view either in Interface Builder (making use of IBOutlet in your view controller's .h file) or in code manually. You hook up your UIWebView the same way you hook up a UIButton or a UILabel.
Shaggy Frog
Well, that's my bad English :) But anyway, after I've made the outlets etc, how will I get the UIWebView from another .m file? Otherwise [myWebView loadRequest:request];won't work.
Knodel
Well, I've connected all the outlets and written this code, but anyway I see blank UIWebView :(
Knodel
Set a breakpoint in `loadFile:`. Does it get hit? What is the value of `myWebView` inside that method? Is it nil? Check the value of `sourceFilePath`. Is it correct? i.e. does the correct HTML file exist at that path?
Shaggy Frog
Yes, the sourceFilePath is correct. I tried to put the loadfile: and loadbar directly into the WebView implementation and it worked (but it was the same bar.html in all of the windows). Actually I'm not sure with one thing. In the UITableView implementation in method loadFile: I use childController.webView, but not webView (because it's implemented in another file). childController is an object of the class, where my webView was implemented. Is it correct? Or should I use delegate methods?
Knodel
I'm not sure if you're saying you subclassed `UITableView` to have a `UIWebView` as a child. If so, that's very strange. Your view controller for your UITableView should be the controller for the UIWebView. Is that not the case?
Shaggy Frog