Swift for OsX WebFrameLoadDelegate doesn't works

*爱你&永不变心* 提交于 2019-12-08 12:25:31

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!