UIWebView 代理混乱

UIWebView Delegate Confusion

在过去的几天里,我一直在研究 UIWebViews 和 Delegates,但我完全感到困惑。我想创建一个包含网络视图的故事板视图控制器。当在 Web 视图中单击按钮时,我想根据对此的回答在视图控制器中触发一些代码:

How to invoke Objective C method from Javascript and send back data to Javascript in iOS?

但是我对如何设置我的委托感到困惑(老实说,我什至不知道什么是委托,它是与 viewcontroller 相关的 webview 吗?)。我一直在阅读和观看一些教程,但我并不真正理解这个过程,而且我认为我做的不对。这是我目前所拥有的。

MyViewController.h

#import <UIKit/UIKit.h>

@protocol MyViewControllerDelegate;

@interface MyViewController : UIViewController
@property (nonatomic, weak) id<MyViewControllerDelegate> delegate;

@property (strong, nonatomic) IBOutlet UIWebView *MyWebView;

@end

@protocol MyViewControllerDelegate <NSObject>

-(bool)myWebView:(UIWebView*)myWebView
shouldStartLoadWithRequest:(NSURLRequest*)request
                  navigationType:(UIWebViewNavigationType)navigationType;

@end

MyViewController.m

#import "MyViewController.h"

@interface MyViewController ()
@end

@implementation MyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [_MyWebView setDelegate:self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewWillAppear:(BOOL)animated
{   
}

@end

我不太确定在哪里实施:

-(bool)myWebView:(UIWebView*)myWebView
shouldStartLoadWithRequest:(NSURLRequest*)request
                  navigationType:(UIWebViewNavigationType)navigationType;

该走了。

如果有人能提供帮助,将不胜感激。

Delegates 提供了不同事件的沟通方式。就像上面的方法一样,当您使用给定的 url 加载 webview 时。

- (void)viewDidLoad {
    [super viewDidLoad];
    [_MyWebView setDelegate:self];
    NSString *urlString = @"http://google.com";
    //Create a URL object.
    NSURL *url = [NSURL URLWithString: urlString];
    //URL Request Object
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    //Load the request in the UIWebView.
    [_MyWebView loadRequest:requestObj];
}

现在使用委托 webview 将与您的控制器通信。当 webview 开始加载新框架时,它将调用您提到的上述方法。此方法的目的是征得您的许可是否应该加载数据?在特殊情况下,如果您不想加载 webView,您可以在此方法中 return no。

还有一些其他委托方法可以让您了解某些阶段,例如如果 webview 已加载您提到的 url 的内容,它将通过调用此方法告诉控制器。

- (void)webViewDidFinishLoad:(UIWebView *)webView{
 //if you want to do anything else after loading the content of webview you can write code here. for example after loading the content you want to show a button on controller you can show it here. 
}

万一 webview 由于 Internet 连接不足或无效 url 或其他原因而无法加载网页或框架。它通过调用另一个委托方法来告诉控制器。

- (void)webView:(UIWebView *)webView didFailLoadWithError:(nullable NSError *)error{

}

首先,您需要了解委托的概念。委托可以是一个对象,当在另一个对象中遇到事件或动作时,它可以代表另一个对象做出反应。

例如,您可以拥有一个 UIViewController,其中包含一个 UITbleView。 UIViewController 可以有一个 UITableViewDelegate。在这种情况下,所有 UITableViewDelegate 方法(heightForRow、ViewForFooter、didSelectRowAtIndexPath、……)都可以通过 UIViewController 实现。因此,例如当一个单元格被选中时(didSelectRowAtIndexPath),UIViewController 可以看到哪个单元格被选中并决定如何处理该选择。

在您的例子中,您的 UIViewController 中有一个 UIWebView 实例。您的 UIViewController 需要成为 UIWebView 的委托。

@interface UIViewController () <UIWebViewDelegate>

然后您在 viewDidLoad 中将 UIWebView 属性 的委托设置为您自己。

[self.webView setDelegate:self];

现在您可以在 viewController 中调用委托方法。

-(bool)myWebView:(UIWebView*)myWebView shouldStartLoadWithRequest (NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType;

在你的视图控制器的 .m 文件中,决定你想用你从网络视图收到的信息做什么。