在Swift 3中以编程方式创建一个没有XIB的NSViewController

在Swift 3中以编程方式创建一个没有XIB的NSViewController,第1张

概述我正在尝试在不使用Interface Builder的情况下制作macOS应用程序.我的项目构建和运行,但我的主视图控制器似乎没有加载其视图.也就是说,不调用viewDidLoad()方法.我正在使用 Xcode-beta 8.0 beta 6(8S201h). NSViewController的Swift 3文档说明了view属性: If this property’s value is not 我正在尝试在不使用Interface Builder的情况下制作macOS应用程序.我的项目构建和运行,但我的主视图控制器似乎没有加载其视图.也就是说,不调用vIEwDIDLoad()方法.我正在使用 Xcode-beta 8.0 beta 6(8S201h).

NSVIEwController的Swift 3文档说明了vIEw属性:

If this property’s value is not already set when you access it,the vIEw controller invokes the loadVIEw() method. That method,in turn,sets the vIEw from the nib file IDentifIEd by the vIEw controller’s nibname and nibBundle propertIEs.

If you want to set a vIEw controller’s vIEw directly,set this property’s value immediately after creating the vIEw controller.

对于vIEwDIDLoad:

For a vIEw controller created programmatically,this method is called immediately after the loadVIEw() method completes.

最后,对于isVIEwLoaded:

A vIEw controller employs lazy loading of its vIEw: Immediately after a vIEw controller is loaded into memory,the value of its isVIEwLoaded property is false. The value changes to true after the loadVIEw() method returns and just before the system calls the vIEwDIDLoad() method.

所以文档让我相信它是可能的.

我的项目看起来像这样:

. ├── main.swift └── Classes     ├── AppDelegate.swift     └── Controllers            └── PrimaryController.swift

main.swift

import Cocoalet delegate = AppDelegate()NSApplication.shared().delegate = delegateNSApplicationMain(Commandline.argc,Commandline.unsafeArgv)

AppDelegate.swift

import Cocoaclass AppDelegate: NSObject,NSApplicationDelegate {    func applicationDIDFinishLaunching(_ aNotification: Notification) {        let pc = PrimaryController(nibname: nil,bundle: nil)!        print(pc.isVIEwLoaded)        pc.vIEw = NSVIEw()        print(pc.isVIEwLoaded)    }}

PrimaryController.swift

import Cocoaclass PrimaryController: NSVIEwController {    overrIDe func loadVIEw() {        print("PrimaryController.loadVIEw")    }    overrIDe func vIEwDIDLoad() {        super.vIEwDIDLoad()        print("PrimaryController.vIEwDIDLoad")    }}

使用上述方法构建和运行项目会产生此控制台输出:

falsetrue

也就是说,视图会加载控制器,但永远不会调用loadVIEw和vIEwDIDLoad方法.

我错过了什么?

解决方法@H_404_92@ 仔细阅读文档是有启发性的.特别:

If this property’s value is not already set when you access it, the vIEw controller invokes the loadVIEw() method.

意思是,只有在属性具有值之前尝试读取视图属性时,才会调用loadVIEw().直接为属性赋值将绕过方法调用.

所以,如果AppDelegate.applicationDIDFinishLaunching(_ :)写成如下:

let pc = PrimaryController(nibname: nil,bundle: nil)!    print(pc.isVIEwLoaded)    let x = pc.vIEw // accessing the vIEw property before it has a value    print(pc.isVIEwLoaded)

…然后系统将调用loadVIEw以尝试加载视图:

falsePrimaryController.loadVIEwfalse

请注意,仍未调用vIEwDIDLoad(),因为没有视图可以自动与控制器关联(即,nib为nil且PrimaryController.xib不存在).完成该过程的正确方法是在PrimaryController.loadVIEw()中手动加载视图:

print("PrimaryController.loadVIEw")vIEw = NSVIEw() // instantiate and bind a vIEw to the controller

现在该程序提供了所需的结果:

falsePrimaryController.loadVIEwPrimaryController.vIEwDIDLoadtrue
总结

以上是内存溢出为你收集整理的在Swift 3中以编程方式创建一个没有XIB的NSViewController全部内容,希望文章能够帮你解决在Swift 3中以编程方式创建一个没有XIB的NSViewController所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1007459.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存