13

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

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Dayzza
  • 1,561
  • 7
  • 18
  • 29

2 Answers2

23

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.

Stan James
  • 2,535
  • 1
  • 28
  • 35
Matthew Frederick
  • 22,245
  • 10
  • 71
  • 97
  • what if i do not have a controller because i am using the windows application template. which only give me two files.. .h and .m. Sorry, i am very new to this..Thanks :) – Dayzza Dec 03 '10 at 03:10
  • Wherever your web view is, that's where you'll listen for the delegate. I'll edit my answer where formatting is better. – Matthew Frederick Dec 03 '10 at 03:32
  • hmmm ok thanks dude, i decided to use view-based app instead... can i ask you one more question? actually my purpose is i want to use the delegate method webViewDidFinishLoad to load finish a webpage first before running some other codes. So where should i put my loading of webpage codes? inside loadView? – Dayzza Dec 03 '10 at 04:06
  • Sure, you can load the UIWebView in your view controller's ViewDidLoad, and then put the code you want to run after the webview has loaded in the webViewDidFinishLoad. (If this answered your question, check the checkmark next to the answer, not this comment.) – Matthew Frederick Dec 03 '10 at 04:16
6

Follow the screen shot below

enter image description here

after connecting inYourViewController.h

@interface inYourViewController : UIViewController<UIWebViewDelegate>

now you are ready to implement the delegate methods in inYourViewController.m file.

or

you can do the same thing from code.

After connecting the webView with view controller via IBOutlet,In inYourViewController.m file's viewDidLoad Method write

self.yourWebView.delegate=self

In inYourViewController.h

@interface inYourViewController : UIViewController<UIWebViewDelegate>

Now you are ready to implement the delegate method of webView.Thanks

Tunvir Rahman Tusher
  • 6,421
  • 2
  • 37
  • 32