如何绕过'self'传递给showWindow方法?

时间:2018-05-02 22:21:06

标签: swift cocoa nswindowcontroller

我正在研究Big Nerd Ranch的Cocoa Programming第5版书中的可可应用程序教程(我在开头的章节中)。在他们blog website讨论这本书的时候,一位用户提到传入“自我”并不是必要的,而且它已经在第18章中介绍了。我现在非常好奇,但是如何在不必重构的情况下对其进行重构。传递'自我'。可能吗?

此代码基本上是创建一个自定义ViewController的实例,需要从AppDelegate加载。

import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    var mainWindowController: MainWindowController?


    func applicationDidFinishLaunching(_ aNotification: Notification) {
        // Insert code here to initialize your application

        let mainWindowController = MainWindowController()

        //put the window of the window controller on the screen
        mainWindowController.showWindow(self)

        //set the property to point to the window controller
        self.mainWindowController = mainWindowController
    }

    func applicationWillTerminate(_ aNotification: Notification) {
        // Insert code here to tear down your application
    }
}

MainWindowController类,如果您需要查看功能。这是非常基本的,它做得不多:

import Cocoa

class MainWindowController: NSWindowController {

    @IBOutlet weak var textField: NSTextField!

    override var windowNibName: NSNib.Name {
        return NSNib.Name.init("MainWindowController")
    }

    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 generatePassword(_ sender: AnyObject) {

        //Get a random string of length 8
        let length = 8

        let password = generateRandomString(length: length)

        //tell the text field to display the string
        textField.stringValue = password

    }


}

0 个答案:

没有答案