Проблемы с передачей делегата из подкласса NSWindowController в мой ViewController - PullRequest
0 голосов
/ 28 декабря 2018

У меня проблемы с передачей пользовательского протокола (MainWindowControllerProtocol) в EditorViewController из MainWindowController, который является подклассом NSWindowController.Пожалуйста, помогите.

EditorViewController.swift

extension EditorViewController: MainWindowControllerProtocol {
    func didOpenFile() {
        print("TODO: Open File") // never called, but it should be
    }
}

class EditorViewController: NSViewController {
    // - IBOutlets
    @IBOutlet weak var treeOutlineView: NSOutlineView!
    @IBOutlet var codeTextView: NSTextView!

    @IBOutlet weak var counterTextField: NSTextField!
    @IBOutlet weak var languageTextField: NSTextField!

    //public var editor = Editor()
    //var rootNode: Node?

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do view setup here.

        //rootNode = Path(Path.userDownloads).node

        // Issue is here
        if let windowController = NSApplication.shared.mainWindow?.windowController as? MainWindowController {
            windowController.delegate = self
        }
        else {
            print("Doesnt work") // prints this
        }

        //treeOutlineView.reloadData()
    }
}

MainWindowController

public protocol MainWindowControllerProtocol {
    func didOpenFile()
}

class MainWindowController: NSWindowController {

    var delegate: MainWindowControllerProtocol?

    override func windowDidLoad() {
        super.windowDidLoad()

        // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
    }
    @IBAction func openFile(_ sender: Any) {
        print("In here") // this is called?
        delegate?.didOpenFile() // but this never is apparently
    }

}

1 Ответ

0 голосов
/ 29 декабря 2018

Может быть эта тема должна помочь.

Этот метод может вернуть nil, если файл пера приложения не завершил загрузку, если приемник не активен или еслиприложение скрыто.

Вы проверили, является ли NSApplication.shared.mainWindow нулем или просто NSApplication.shared.mainWindow?.windowController не может быть приведен к вашему классу контроллеров?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...