views:

338

answers:

1

I have a navigation-based application with 2 UIViewControllers (controller-2 is pushed onto controller-1 when the user selects a particular row within a UITableView displayed using controller-1).

Here's the 'twist'… When controller-1 is loaded, I want to "prefetch" a URL containing HTML/javascript into a UIWebView. Initially, the user won't see this UIWebView. Instead they will see a UITableView allowing them to click on 5 different options (row0, row1, row2, row3, row4).

When the user selects a row I would like to push a new view onto the stack and "pass" the HTML/Javascript to a UIWebView located within this new view, and display the web-page in the new view.

Is it possible to pass the UIWebView like this?

I've tried the following, which is not displaying the HTML within UIWebView, so any suggestions on how to pass the UIWebView (or alternatives) would be great. I'm pretty sure I've made the correct connections within my nib file for controller2.

To explain my motives. I want to use javascript to hide certain elements of the web-page depending on which row the user clicks. To me, it's faster and easier to make 1 URL request and then use javascript to hide various HTML elements than it is to make 5 different URL requests, fetching 5 separate web-pages. So, if there are alternative thoughts to this approach, that would be great, too. I thought about doing this all within 1 viewcontroller (adding and removing subviews), but I'd rather push onto a new view to take advantage of the navigation controller.

TYVM!!!!

////////// controller 1 //////////////////////

- (void)viewWillAppear:(BOOL)animated {
  // do other stuff ///
  NSURLRequest    *request_object = [NSURLRequest requestWithURL:self.url];
  [self.web_view1 loadRequest:request_object];         
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    self.view_controller2             = [[[ViewController2 alloc]   initWithNibName:@"ViewController2" bundle:nil] autorelease];
    self.view_controller2.web_view2   = self.web_view1;
    [self.navigationController        pushViewController:self.view_controller2 animated:YES];
}
//////////// controller 1 //////////////////////

//

///////////// portion of controller 2 header file /////////////////////////

@interface ViewController2 : UIViewController {
    IBOutlet UIWebView *web_view2;
}
@property (nonatomic, retain) IBOutlet UIWebView *web_view2;


//////////// portion of controller 2 implementation file //////////////
#import "ViewController2.h"

@implementation ViewController2
@synthesize web_view2;

- (void)dealloc {
    [web_view2 release], web_view2 = nil;
    [super dealloc];
}

//////////// controller 2 //////////////////////////
+1  A: 

You've got the right idea, but you need to do one other thing. In your -tableView:didSelectRowAtIndexPath:. you need to add the webview to the viewController's view, not just assign it to an outlet. The following might work, depending on your XIB files:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  self.view_controller2 = [[[ViewController2 alloc] initWithNibName:@"ViewController2" bundle:nil] autorelease];
  self.web_view1.frame = self.view_controller2.web_view2.frame;
  [self.view_controller2.web_view2 removeFromSuperview];
  self.view_controller2.web_view2 = self.web_view1;
  [self.view_controller2.view addSubview: self.web_view1];
  [self.navigationController pushViewController:self.view_controller2 animated:YES];
}

This assumes that web_view2 is a valid and hooked up outlet, containing a webview in the location you want your 'cached' view to be.

Ben Gottlieb