我正在尝试以编程方式在 macOS 应用程序中显示一个窗口。我想以编程方式执行此操作的原因是因为用户单击“登录”按钮,结果函数取决于登录是否成功。如果成功,则会显示该窗口;否则,就不是。
这是我的 Xcode 窗口的屏幕截图:
这是我在代码中尝试的内容(Alert是我创建的一个类,旨在使显示NSAlert对话框更容易):
@IBAction func btnLogin_Click(_ sender: Any) {
let email = txtEmail.stringValue
if(email.isEmpty) {
Alert.show(parent: view.window!, message: "Email is required.")
return
}
let password = txtPassword.stringValue
if(password.isEmpty) {
Alert.show(parent: view.window!, message: "Password is required.")
return
}
// this does not work, even though I gave the window and view controllers
// both identifiers in the storyboard
self.storyboard?.instantiateController(withIdentifier: NSStoryboard.SceneIdentifier("wcStores"))
}
Run Code Online (Sandbox Code Playgroud)
Apple 的文档NSStoryboard.SceneIdentifier几乎不存在,而且我见过的其他方法似乎与 Swift 的早期版本有关。
我究竟做错了什么?
好吧,这不是我最初想要做的,但这种方式要好得多,也是我真正想要的。
let vcStores = self.storyboard?.instantiateController(withIdentifier: NSStoryboard.SceneIdentifier("vcStores"))
as! NSViewController
self.view.window?.contentViewController = vcStores
Run Code Online (Sandbox Code Playgroud)
这只会将窗口的内容替换为 Interface Builder 中定义的视图中的内容vcStores。
该视频也有帮助,尽管是针对 iOS 和 Swift 3。我一开始尝试创建一个新的NSWindow,因为我已经习惯了在 Java Swing 或 C# Windows Forms 中进行桌面开发。但我喜欢这样轻松地切换窗口内容。
| 归档时间: |
|
| 查看次数: |
5378 次 |
| 最近记录: |