swift - 如何从窗口 Controller 中引用 View ?

标签 swift xcode macos

我有一个带工具栏的窗口 Controller 。我还有一个包含一些 View 的 View Controller 。如何在我的窗口 Controller 中引用来自 View Controller 的 View ?我仍在学习 macOS 开发,我没有了解代码的结构和类的交互方式。

我现在的具体问题是:使用 XCode 9.4.1 我有一个带有工具栏和按钮的窗口。这就是我的 WindowsController.swift 的样子:

import Cocoa

class WindowController: NSWindowController {
    override func windowDidLoad() {
        super.windowDidLoad()
        window?.titleVisibility = .hidden
    }

    @IBAction func startExport(_ sender: NSButton) {
        print("Start Export")
    }
}

在 ViewControllerScene 中有一个正在加载网页的 WKWebView。当按下工具栏中的按钮时,我想调用该 Web View 的 takeSnapshot 方法。因此,我需要在 WindowsController.swift 中引用该 Web View ,但是通过控制将 Web View 从 Storyboard拖动到助理编辑器中的 WindowsController.swift 不允许我创建该导出。

最佳答案

这个:

let vc = contentViewController as? ViewController

将带您到您的 View Controller 。

关于swift - 如何从窗口 Controller 中引用 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51398632/

相关文章:

php - 升级 php 后 MySQL 无法在 mac Sierra 上运行

ios - UITableView 中包含 UIView 的滚动问题

Swift - 禁用辅助功能画外音?

ios - SpriteKit 错误地检测到多重碰撞

ios - Xcode iOS 模拟器允许我的 Cordova 应用程序检索地理位置,但观看位置出现超时错误

css - 适用于 Mac 的以绘图为中心的 CSS/Web 设计应用程序好吗?

macos - 如何在 macOS SwiftUI 中创建默认按钮?

json - 无法将字符串类型的值转换为预期的参数类型 Int

ios - 如何在 Swift 4 中使用 FetchedResultsController 获取 DateSectionTitles

ios - 加载新 View 时如何禁用 UISwipeGestureRecognizer?