UIWebView和导航控制器

Nic*_* AD 4 iphone xcode objective-c uiwebview uinavigationcontroller

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

我找到了这个帖子,但是我很想知道在哪里实现那段代码(我是Objective-C/IPhone的新手)

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

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

#import <UIKit/UIKit.h>


@interface ContactViewController : UIViewController {

    IBOutlet UIWebView *webView1;
}

@property (nonatomic, retain) UIWebView *webView1;
Run Code Online (Sandbox Code Playgroud)

Nat*_*nes 6

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

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

UIViewController <UIWebViewDelegate>
Run Code Online (Sandbox Code Playgroud)

在.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;}
Run Code Online (Sandbox Code Playgroud)