Knodel
Knodel

Reputation: 4389

How to make same UIWebView take data from different local .html files?

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!

Upvotes: 2

Views: 969

Answers (1)

Shaggy Frog
Shaggy Frog

Reputation: 27601

- (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];
}

Upvotes: 2

Related Questions