当 UIWebView 请求开始加载时,不会调用自定义 UIWebViewDelegate 的 ShouldStartLoad [英] ShouldStartLoad of custom UIWebViewDelegate not being called when UIWebView request starts loading
问题描述
我已经定义了一个 UIWebViewDelegate 类并在我的 UIWebView 的 Delegate 属性中实例化它,但是当 webview 加载请求时没有调用 ShouldStartLoad 方法.我究竟做错了什么?
这是定义我的 UIWebViewDelegate 和我的 UIWebView 的代码:
public class MyWebViewDelegate: UIWebViewDelegate{私有 UIWebView _view;公共 MyWebViewDelegate(UIWebView 视图){_view = 视图;}public override bool ShouldStartLoad (UIWebView webView,MonoTouch.Foundation.NSUrlRequest 请求,UIWebViewNavigationType 导航类型){System.Console.WriteLine("开始加载");返回真;}}公共类 MyWebView : UIWebView{私有静态 MyWebView _instance = new MyWebView();私有 MyWebView () : base(){this.Delegate = new MyWebViewDelegate(this);}公共静态 MyWebView 实例 {得到{返回_实例;}}公共无效负载(){this.LoadRequest (new NSUrlRequest(NSUrl.FromString("http://myurl"),NSUrlRequestCachePolicy.ReloadIgnoringLocalAndRemoteCacheData,5));}}
在我的 appdelegate 中,我将在 FinishedLaunching 方法中执行以下操作:
//创建表单视图控制器viewControllerForm = new UIViewController();viewControllerForm.View = MyWebView.Instance;viewControllerForm.Title = Localization.Translate("Forms");//加载网页视图MyWebView.Instance.Load();
请注意,我已使用复制/粘贴来提供此代码示例,因此此处可能存在一些小的复制/粘贴错误,但我的代码正在编译并且 webview 加载了请求;只是 ShouldStartLoad 没有被调用.
有人知道我在这里做错了什么吗?
我通过使用 webview 的 ShouldStartLoad 属性解决了这个问题,如下所示:
myView.ShouldStartLoad = (webView, request, navType) =>{//在这里确定要做什么}
我从 miguel.de.icaza 对 此处.>
I have defined a UIWebViewDelegate class and instantiated it in the Delegate property of my UIWebView, but the ShouldStartLoad method is not being called when the webview loads a request. What am I doing wrong?
Here is the code defining my UIWebViewDelegate and my UIWebView:
public class MyWebViewDelegate: UIWebViewDelegate
{
private UIWebView _view;
public MyWebViewDelegate (UIWebView view)
{
_view = view;
}
public override bool ShouldStartLoad (UIWebView webView,
MonoTouch.Foundation.NSUrlRequest request,
UIWebViewNavigationType navigationType)
{
System.Console.WriteLine("Starting load");
return true;
}
}
public class MyWebView : UIWebView
{
private static MyWebView _instance = new MyWebView ();
private MyWebView () : base()
{
this.Delegate = new MyWebViewDelegate(this);
}
public static MyWebView Instance {
get { return _instance; }
}
public void Load ()
{
this.LoadRequest (new NSUrlRequest(NSUrl.FromString("http://myurl"),
NSUrlRequestCachePolicy.ReloadIgnoringLocalAndRemoteCacheData,
5)
);
}
}
In my appdelegate I am then doing the following in FinishedLaunching method:
// Create form view controller
viewControllerForm = new UIViewController();
viewControllerForm.View = MyWebView.Instance;
viewControllerForm.Title = Localization.Translate("Forms");
// Load webview
MyWebView.Instance.Load();
Note that I have used copy/paste to give this code sample, so there could be some minor copy/paste errors here, but my code is compiling and the webview loads the request; it is just that the ShouldStartLoad that is not being called.
Does anyone have any idea what I am doing wrong here?
I solved this by making use of the ShouldStartLoad property of the webview as follows:
myView.ShouldStartLoad = (webView, request, navType) => {
// Determine here what to do
}
I got this answer from miguel.de.icaza's answer to the question posted here.
这篇关于当 UIWebView 请求开始加载时,不会调用自定义 UIWebViewDelegate 的 ShouldStartLoad的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!