iphone – UIWebView和导航控制器

前端之家收集整理的这篇文章主要介绍了iphone – UIWebView和导航控制器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何使用UINavigationController告诉我的UIWebView在新视图中打开链接

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

Clicking a link in UIWebView pushes onto the NavigationView stack

我的视图只包含一个带有UINavigationController的简单WebView

#import <UIKit/UIKit.h>


@interface ContactViewController : UIViewController {

    IBOutlet UIWebView *webView1;
}

@property (nonatomic,retain) UIWebView *webView1;

解决方法

假设您的视图控制器(包含webview)已经在导航控制器中,这里有一些步骤.

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

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

猜你在找的Xcode相关文章