Uiwebview导航和重定向

时间:2011-05-20 22:01:41

标签: uiwebview uinavigationcontroller navigation redirect

文本视图中显示的任何链接都会自动突出显示并显示为链接并被重定向到safari,但我希望将其重定向到我为浏览创建的Uiwebview。这样我就不会离开我的应用程序了。那么如何将我的页面链接到uiwebview页面。

谢谢。

1 个答案:

答案 0 :(得分:0)

创建一个加载.xib的UIWebView类(WebViewController),看起来像这样:

#import <UIKit/UIKit.h>

@interface WebViewController : UIViewController
{

}

@property (nonatomic, retain) IBOutlet UIWebView *webView;
@property (nonatomic, copy) NSString *urlString;

@end

然后在实现文件中,实现此方法:

-(void)viewWillAppear:(BOOL)animated
{
    NSURL *url = [NSURL URLWithString:urlString_];

    NSURLRequest *req = [NSURLRequest requestWithURL:url];

    [[self webView] loadRequest:req];
    NSLog(@"url requested");

    [[self navigationController] setNavigationBarHidden:NO animated:YES];
    NSLog(@"showing nav");
    [super viewWillAppear:animated];
}

然后在实例化Web视图的委托类中,将WebViewController提供给url并将其推送到导航控制器堆栈:

netView.urlString = @"http://www.morrisjenkins.com/home";
[[self navigationController] pushViewController:netView animated:YES];
[netView release];
netView = nil;

(这假设您将委托类添加到应用程序委托中的UINavigationController)

UIViewController *webController = [[WebViewDelegateController alloc] init];

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:webController];