UIWebView 和导航控制器

发布于 2024-10-24 17:54:47 字数 621 浏览 1 评论 0原文

如何使用 UINavigationController 告诉我的 UIWebView 在新 View 中打开链接?

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

点击 UIWebView 中的链接推送到 NavigationView 堆栈

我的视图只包含一个简单的 WebViewUINavigationController< /代码> 在顶部

#import <UIKit/UIKit.h>


@interface ContactViewController : UIViewController {

    IBOutlet UIWebView *webView1;
}

@property (nonatomic, retain) UIWebView *webView1;

How can I tell my UIWebView to open links in a new View , using a 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)

Clicking a link in UIWebView pushes onto the NavigationView stack

my view just contains a simple WebView with a UINavigationController on top

#import <UIKit/UIKit.h>


@interface ContactViewController : UIViewController {

    IBOutlet UIWebView *webView1;
}

@property (nonatomic, retain) UIWebView *webView1;

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

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

发布评论

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

评论(2

两相知 2024-10-31 17:54:47

以下步骤假设您的视图控制器(包含 Web 视图)已经位于导航控制器内。

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

UIViewController <UIWebViewDelegate>

在 .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;}

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

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

UIViewController <UIWebViewDelegate>

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

- (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;}
━╋う一瞬間旳綻放 2024-10-31 17:54:47

我对 Objective C / iPhone 也很陌生,但我会尝试,..

为你的 webview 创建一个新的 viewController 并将其推送到你的 UINavigator 中

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

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

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

yourWebViewController *y =  [[yourWebViewController alloc] init];
[self.navigationController pushViewController:y animated:YES];
[y release];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文