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

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

问题描述

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



这是我的应用程式delegate.swift档案:

  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委托类:

  import WebKit 

class WebViewControllerDelegate:WebFrameLoadDelegate {

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

还有我不知道如何设置这个委托到我的WebView。

一般来说, WebViewControllerDelegate



移动此代码:

 

code> @IBOutlet var webview:WebView!

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

添加到您的 WebViewControllerDelegate



您还需要在 WebViewControllerDelegate



<$ p中为WebView设置委托$ p> self.webView.delegate = self

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



最后,你需要确保你的类符合WebFrameLoadDelegate协议(你已经做了) 。

  class WebViewControllerDelegate:WebFrameLoadDelegate {

上面的代码意味着你有一个viewController,它有一个webView属性,符合正确的代理协议并且本身是webView的委托。



然后会调用 WebViewControllerDelegate 中定义的委托方法。


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.

解决方案

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.

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

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