xcode UIWebView 和导航控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5380200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:36:06  来源:igfitidea点击:

UIWebView and navigation controller

iphoneobjective-cxcodeuiwebviewuinavigationcontroller

提问by Nico AD

How can I tell my UIWebViewto open links in a new View , using a UINavigationController?

如何告诉我UIWebView在新视图中打开链接,使用UINavigationController?

I found this thread, but i m a little confused of where to implement that piece of code (i m new to objective-C/IPhone)

我找到了这个线程,但我对在哪里实现那段代码有点困惑(我是 Objective-C/IPhone 的新手)

Clicking a link in UIWebView pushes onto the NavigationView stack

单击 UIWebView 中的链接会推送到 NavigationView 堆栈上

my view just contains a simple WebViewwith a UINavigationControlleron top

我的观点只包含一个简单WebViewUINavigationController顶部

#import <UIKit/UIKit.h>


@interface ContactViewController : UIViewController {

    IBOutlet UIWebView *webView1;
}

@property (nonatomic, retain) UIWebView *webView1;

回答by Nathan Jones

Here are steps assuming your view controller (that houses the webview) is already within a navigation controller.

以下是假设您的视图控制器(包含 webview)已经在导航控制器中的步骤。

Within the .h - ensure your view controller conforms to the webview delegate

在 .h 中 - 确保您的视图控制器符合 webview 委托

UIViewController <UIWebViewDelegate>

Within the .m - add the following code (or just implement this method with whatever logic you want)

在 .m 中 - 添加以下代码(或仅使用您想要的任何逻辑实现此方法)

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
    YourNextViewController *ynvc = [[[YourNextViewController alloc] initWithNibName:@"YourNextViewController" bundle:nil] autorelease];
    ynvc.ivar1 = value1;
    ynvc.ivar2 = value2;

    UIBarButtonItem *backButton = [[[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:nil action:nil] autorelease];
    [[self navigationItem] setBackBarButtonItem:backButton];

    [self.navigationController pushViewController:ynvc animated:YES];

    return NO;
}
return YES;}

回答by Marc

I'm pretty new to objective C / iPhone too, but what I would try,..

我对目标 C/iPhone 也很陌生,但是我会尝试什么,..

Is creating a new viewController for your webview and push it in your UINavigator

正在为您的 webview 创建一个新的 viewController 并将其推送到您的 UINavigator

yourWebViewController *y =  [[yourWebViewController alloc] init];
[self.navigationController pushViewController:y animated:YES];
[y release];