Skip to main content
 首页 » 操作系统

xcode之为什么 WebFrameLoadDelegate 被视为未声明

2024年01月01日37lvdongjie

我有一个基本的 Cocoa 应用程序,其窗口中只有一个 WebView。 WebKit.framework 引用被添加到 OS X 10.9 (Mavericks) 上的 Xcode 6 beta 6 项目中。我想知道 WebView 何时完成页面加载。所以我创建了一个继承WebFrameLoadDelegate的WebViewControllerDelegate类。这已经是问题开始的地方:Xcode 告诉我 Use of undeclared type 'WebFrameLoadDelegate'。关于 this question on Stack Overflow ,它不应该。如前所述,WebKit.framework 由项目引用,WebKit 模块在 Swift 类文件中导入。我还在 Xcode 左侧栏的“Headers”文件夹中看到了 WebFrameLoadDelegate.h,位于 WebKit.framework 下方。

万恶之源是 TestWebView/WebViewControllerDelegate.swift 文件中的 11 行(我忘了删除这里省略的注释),类声明和协议(protocol)引用。

import WebKit 
 
class WebViewControllerDelegate: WebFrameLoadDelegate { 
    func didFinishLoadForFrame() { 
        NSLog("didFinishLoadForFrame()") 
    } 
} 

AppDelegate,设置一切:

import Cocoa 
import WebKit 
 
class AppDelegate: NSObject, NSApplicationDelegate { 
    @IBOutlet weak var window: NSWindow! 
    @IBOutlet weak var webView: WebView! 
    @IBOutlet weak var webViewControllerDelegate: WebViewControllerDelegate! 
 
    func applicationDidFinishLaunching(aNotification: NSNotification?) { 
        self.webView.frameLoadDelegate = self.webViewControllerDelegate 
        self.webView.mainFrame.loadRequest(NSURLRequest(URL: NSURL(string: "https://stackoverflow.com/"))) 
    } 
 
    func applicationWillTerminate(aNotification: NSNotification?) {} 
} 

有关更多信息,请查看我的 my public repository 中的代码这是我为这个问题创建的。

请您参考如下方法:

WebFrameLoadDelegate 是一种非正式协议(protocol),因此您不能声明遵守它。只需继承 NSObject 并实现所需的方法即可。

另外,请注意 Swift 方法名称是 webView(sender: WebView!, didFinishLoadForFrame frame: WebFrame!),而不仅仅是 didFinishLoadForFrame()。

class WebViewControllerDelegate: NSObject { // WebFrameLoadDelegate 
      override func webView(sender: WebView!, didFinishLoadForFrame frame: WebFrame!) { 
      NSLog("didFinishLoadForFrame()") 
    } 
}