English 中文(简体)
ShouldStartLoad of custom UIWebViewDelegate not being called when UIWebView request starts loading
原标题:

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.

问题回答

Don t subclass UIWebView. See the UIWebView Class Reference - Subclassing Notes: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIWebView_Class/Reference/Reference.html





相关问题
Write to a File in Monotouch

How would I create and write to a file in a Monotouch iPhone app? The file should persist between application launches, so I guess it has to be placed somewhere in the App bundle ( documents or ...

MonoTouch debugger never connects to app

I have an issue where the MonoDevelop debugger refuses to connect to the simulator. MonoDevelop pops a box saying "Waiting for debugger to connect on 127.0.0.1:10000..." but never connects. I have ...

montouch detect when sound finished playing

In objective C is a method called AudioServicesAddSystemSoundCompletion which allows to add a handler for "audio done notification". So I add the handler call AudioServicesPlaySystemSound and when the ...

Web Services Build Error in Monotouch

My project and web services work fine in the simulator but crash when uploading to an iPhone. It had been working fine before I updated Monotouch from 1.1. Can I revert back to earlier versions of ...

Playing a Sound With Monotouch

No matter what I try (build -> content, NSUrl, filename) I get a null exception : file not found when I try to play a .caf sound file in monotouch. //var path = NSBundle.MainBundle.PathForResource("...

Is MonoDevelop s editor slow for you?

I m using the evaluation version of MonoTouch and MonoDevelop to check out writing iPhone app in C#. However, MonoDevelop s editor is very slow for me. I mean really really slow. I have to type and ...

C# Class Definition

In XCode and objective c, if I want to use a UIGlassButton in interface builder, I put the following line in my header file: @class UIGlassButton; I am now using monotouch c#. Is there a similar ...

热门标签