set a delegate for UIWebView

Dayzza picture Dayzza · Dec 3, 2010 · Viewed 25.3k times · Source

How do I set a delegate for my UIWebView to use webViewDidFinishLoad?

Answer

Matthew Frederick picture Matthew Frederick · Dec 3, 2010

In the interface file indicate that you're interested:

@interface SomethingController : UIViewController <UIWebViewDelegate> {


Then in your implementation you'll add that delegate method:

- (void)webViewDidFinishLoad:(UIWebView *)webView {

    // Do whatever you want here

}

Obviously SomethingController should be your actual controller, and UIViewController is whatever you're actually implementing.

This, by the way, is basically how you implement any delegate.

Edit

In a window-based app, assuming your UIWebView is in your app delegate, you just add it to the list of delegates, separated by commas, like this:

@interface YourAppDelegate : NSObject <UIApplicationDelegate, UIWebViewDelegate>

You'll include your webViewDidFinishLoad in the app delegate's implementation.

Edit 2

Finally, you'll need to connect your UIWebView to your controller. It's common to do this in the viewDidLoad method of a UIViewController, with a line like this:

self.yourWebView.delegate = self;

This tells the UIWebView where it should send messages.