ios 为 UIWebView 设置一个委托
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4342006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
set a delegate for UIWebView
提问by Dayzza
How do I set a delegate for my UIWebView
to use webViewDidFinishLoad
?
如何设置一个委托供我UIWebView
使用webViewDidFinishLoad
?
回答by Matthew Frederick
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 SomethingControllershould be your actual controller, and UIViewControlleris whatever you're actually implementing.
很明显,SomethingController应该是你的实际控制器,而UIViewController是你实际实现的任何东西。
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:
在基于窗口的应用程序中,假设您的 UIWebView 在您的应用程序委托中,您只需将其添加到委托列表中,以逗号分隔,如下所示:
@interface YourAppDelegate : NSObject <UIApplicationDelegate, UIWebViewDelegate>
You'll include your webViewDidFinishLoadin the app delegate's implementation.
您将在应用程序委托的实现中包含您的webViewDidFinishLoad。
Edit 2
编辑 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:
最后,您需要将 UIWebView 连接到控制器。在viewDidLoad
UIViewController的方法中这样做是很常见的 ,有这样一行:
self.yourWebView.delegate = self;
This tells the UIWebView where it should send messages.
这会告诉 UIWebView 它应该在哪里发送消息。
回答by Tunvir Rahman Tusher
Follow the screen shot below
按照下面的屏幕截图
after connecting inYourViewController.h
连接后 inYourViewController.h
@interface inYourViewController : UIViewController<UIWebViewDelegate>
now you are ready to implement the delegate methods in inYourViewController.m
file.
现在您已准备好在inYourViewController.m
文件中实现委托方法。
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
通过视图控制器连接 webView 后IBOutlet
,在inYourViewController.m
文件的viewDidLoad
方法中写入
self.yourWebView.delegate=self
In inYourViewController.h
在 inYourViewController.h
@interface inYourViewController : UIViewController<UIWebViewDelegate>
Now you are ready to implement the delegate method of webView.Thanks
现在你已经准备好实现 webView 的委托方法了。谢谢