Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load a UIWebView with a close button on top?

I currently have the following code that loads a UIWebView from another View. Now is there anyway I can have a close button?

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    UIWebView *webView=[[UIWebView alloc] initWithFrame:CGRectMake(0,0,320,480)];
    [self.view addSubview:webView];
    NSURLRequest *urlRequest;
    NSURL *urlforWebView;
    urlforWebView=[NSURL URLWithString:@"http://www.google.com"];
    urlRequest=[NSURLRequest requestWithURL:urlforWebView];
    [webView loadRequest:urlRequest];

}

I am going to load a page built using jquery mobile, so a close button inside the page would also work fine. But on a navigation bar would be ideal. Btw, my application does not have a UINavigationBar

like image 990
gk1 Avatar asked Dec 05 '22 00:12

gk1


1 Answers

I would create a new sub class of UIViewController, say WebViewController with a nib. Then I would add an UINavigationBar with a close button and an UIWebView. Then to show your web view controller you can do something like:

WebViewController *webViewController = [[WebViewController alloc] init];
webViewController.loadURL = [NSURL URLWithString:@"http://www.google.com"];
[self presentModalViewController:webViewController animated:YES];
[webViewController release];

In your WebViewController you can define:

@property (nonatomic, retain) IBOutlet UIWebView *webView;
@property (nonatomic, retain) NSURL *loadURL;

- (IBAction)close:(id)sender;

and implement something like this:

- (void)viewDidLoad {
  [super viewDidLoad]

  NSURLRequest *urlRequest = [NSURLRequest requestWithURL:self.loadURL];
  [self.webView loadRequest:urlRequest];
}

- (IBAction)close:(id)sender {
  [self dismissModalViewControllerAnimated:YES];
}
like image 110
Robert Höglund Avatar answered Dec 19 '22 19:12

Robert Höglund