简体   繁体   中英

UIWebView and navigation controller

How can I tell my UIWebView to open links in a new View , using a UINavigationController ?

I found this thread, but ima little confused of where to implement that piece of code (im 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;

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM