Я работаю над учебным пособием по применению какао из книги 5-го издания «Программирование какао» Большого ботаника (я в первых главах). На своем блоге для обсуждения книги пользователь упоминает, что переход к «я» не является необходимым и что он описан в главе 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
}
}