问题
I am new in OSX developing and i am new in Swift. I want to write simple app with WebView. I can load url, but I need to catch event when, WebView end loading content.
it's my app delegate.swift file:
import WebKit
class AppDelegate: NSObject, NSApplicationDelegate{
@IBOutlet weak var window: NSWindow!
@IBOutlet var webview: WebView!
func applicationDidFinishLaunching(aNotification: NSNotification?) {
let url = NSURL(string: "google.com")
let request = NSURLRequest(URL: url);
webview.mainFrame.loadRequest(request);
}
func applicationWillTerminate(aNotification: NSNotification?) {
}
}
and i try to make WebFrameLoad delegate class:
import WebKit
class WebViewControllerDelegate: WebFrameLoadDelegate{
@IBOutlet var webview: WebView!
override func didFinishLoadForFrame()
{
println("ok:");
}
}
It doesn't work. And also i don't know how to set this Delegate to my WebView. TY for answers.
回答1:
Generally speaking the WebViewControllerDelegate
would be the class to have the webView as a property.
Move this code:
@IBOutlet var webview: WebView!
let url = NSURL(string: "google.com")
let request = NSURLRequest(URL: url)
self.webview.mainFrame.loadRequest(request)
to your WebViewControllerDelegate
You then also need to set the delegate for the WebView in your WebViewControllerDelegate
self.webView.delegate = self
If you don't set this, then the system will not call the delegate methods for you.
Finally you need to ensure that your class conforms to the WebFrameLoadDelegate protocol (which you have already done).
class WebViewControllerDelegate: WebFrameLoadDelegate{
The above means that you have a viewController which has a webView property, is conforming to the correct delegate protocol, and is itself the delegate for the webView.
Delegate methods defined inside the WebViewControllerDelegate
will then be called for you.
来源:https://stackoverflow.com/questions/25199641/swift-for-osx-webframeloaddelegate-doesnt-works