本文介绍了如何使用顶部的关闭按钮加载UIWebView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有以下代码从另一个视图加载UIWebView。现在有没有我可以关闭按钮?

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

}

我要加载一个使用jquery mobile构建的页面,所以页面内的关闭按钮也可以正常工作。但在导航栏上是理想的。顺便说一下,我的应用程序有一个UINavigationBar

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

推荐答案

我会创建一个新的子类 UIViewController ,用一个笔尖说 WebViewController 。然后我将添加一个 UINavigationBar ,其中包含一个关闭按钮和一个 UIWebView 。然后,为了显示您的Web视图控制器,您可以执行以下操作:

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

WebViewController 中,您可以定义:

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

- (IBAction)close:(id)sender;

并执行以下内容:

- (void)viewDidLoad {
  [super viewDidLoad]

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

- (IBAction)close:(id)sender {
  [self dismissModalViewControllerAnimated:YES];
}

这篇关于如何使用顶部的关闭按钮加载UIWebView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 22:50