UIWebView和导航控制器

时间:2011-03-21 15:55:18

标签: iphone objective-c xcode uiwebview uinavigationcontroller

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

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

Clicking a link in UIWebView pushes onto the NavigationView stack

我的观点只包含一个简单的WebView,其顶部有UINavigationController

#import <UIKit/UIKit.h>


@interface ContactViewController : UIViewController {

    IBOutlet UIWebView *webView1;
}

@property (nonatomic, retain) UIWebView *webView1;

2 个答案:

答案 0 :(得分:6)

以下步骤假定您的视图控制器(包含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;}

答案 1 :(得分:0)

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

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

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