如何加载顶部有关闭按钮的 UIWebView?

发布于 2024-10-04 20:51:17 字数 628 浏览 2 评论 0原文

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

- (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 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

末骤雨初歇 2024-10-11 20:51:17

我将创建一个新的 UIViewController 子类,例如带有笔尖的 WebViewController 。然后我将添加一个带有关闭按钮的 UINavigationBar 和一个 UIWebView。然后,要显示您的 Web 视图控制器,您可以执行以下操作:

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

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];
}
杀手六號 2024-10-11 20:51:17

每当加载 UIWebView 时,您都可以先在内容上运行 javascript 片段。

[webView stringByEvaluatingJavaScriptFromString:@"window.close = function() { window.location = 'app://close-webview'; };"];

这只是劫持了 window.close() 的正常行为,让您有机会在 Objective-C 中捕获该调用。

然后在你的 UIWebViewDelegate 中你可以监听你选择作为关闭 url 的任何内容。

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    if([request.URL.absoluteString isEqualToString:@"app://close-webview"]) {
        webView.hidden = YES;
        // or whatever you want to do to remove the UIWebView...
        return NO;
    }
    return YES;
}

有点 hackish,但它允许 Web 开发人员从 HTML 内容中控制关闭按钮的外观和感觉。

Whenever you load the UIWebView, you could run a javascript snippet on the content first.

[webView stringByEvaluatingJavaScriptFromString:@"window.close = function() { window.location = 'app://close-webview'; };"];

That just hijacks the normal behavior of window.close() and gives you chance to catch the call in your Objective-C.

Then in your UIWebViewDelegate you could listen for whatever you chose as the close url.

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    if([request.URL.absoluteString isEqualToString:@"app://close-webview"]) {
        webView.hidden = YES;
        // or whatever you want to do to remove the UIWebView...
        return NO;
    }
    return YES;
}

A bit hackish, but it would allow the web developer to control the look and feel of the close button from within the HTML content.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文