UIWebView and navigation controller

How can I tell my UIWebView to open links in a new view using the UINavigationController ?

I found this thread, but I'm a little confused about where to implement this piece of code (im new for objective-C / IPhone)

Clicking a link in a UIWebView, clicks on 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; 
+4
source share
2 answers

Here are the steps assuming that your view controller (which hosts the webview) is already in the navigation controller.

Inside .h - make sure your view controller matches the webview delegate

 UIViewController <UIWebViewDelegate> 

Inside .m - add the following code (or just implement this method using any logic)

 - (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;} 
+6
source

I'm also pretty new to objective C / iPhone, but I would try, ..

Creates a new viewController for your web browser and clicks it in your UINavigator

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

Source: https://habr.com/ru/post/1344626/


All Articles