Swift for OsX WebFrameLoadDelegate 不起作用 [英] Swift for OsX WebFrameLoadDelegate doesn't works

查看:26
本文介绍了Swift for OsX WebFrameLoadDelegate 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 OSX 开发新手,我是 Swift 新手.我想用 WebView 编写简单的应用程序.我可以加载 url,但我需要在 WebView 结束加载内容时捕获事件.

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.

这是我的应用程序 delegate.swift 文件:

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?) {
    }
}

我尝试制作 WebFrameLoad 委托类:

and i try to make WebFrameLoad delegate class:

import WebKit

class WebViewControllerDelegate: WebFrameLoadDelegate{

    @IBOutlet var webview: WebView!    
    override func didFinishLoadForFrame()
    {
        println("ok:");
    }
}

它不起作用.而且我不知道如何将此委托设置为我的 WebView.TY 的答案.

It doesn't work. And also i don't know how to set this Delegate to my WebView. TY for answers.

推荐答案

一般来说,WebViewControllerDelegate 是将 webView 作为属性的类.

Generally speaking the WebViewControllerDelegate would be the class to have the webView as a property.

移动这个代码:

@IBOutlet var webview: WebView!

let url = NSURL(string: "google.com")
let request = NSURLRequest(URL: url)
self.webview.mainFrame.loadRequest(request)

到你的 WebViewControllerDelegate

然后您还需要在 WebViewControllerDelegate

self.webView.delegate = self

如果你不设置这个,那么系统不会为你调用委托方法.

If you don't set this, then the system will not call the delegate methods for you.

最后,您需要确保您的类符合 WebFrameLoadDelegate 协议(您已经完成了).

Finally you need to ensure that your class conforms to the WebFrameLoadDelegate protocol (which you have already done).

class WebViewControllerDelegate: WebFrameLoadDelegate{

以上意味着您有一个具有 webView 属性的 viewController,它符合正确的委托协议,并且它本身是 webView 的委托.

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.

然后会为您调用 WebViewControllerDelegate 中定义的委托方法.

Delegate methods defined inside the WebViewControllerDelegate will then be called for you.

这篇关于Swift for OsX WebFrameLoadDelegate 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆