【发布时间】:2019-03-08 15:01:33
【问题描述】:
简介:
我想创建一个在首次启动时显示一些“用户指南”的应用程序,我已经完成了检测应用程序是否首次启动的部分,但是当涉及到设置视图控制器部分时,我可以不知道怎么做。
现状:
我已经做了很多研究,也看到了很多关于“如何在 swift 中以编程方式设置初始视图控制器”等主题的 Q/A 文章,但基本上都是以 ios 开发为例,没有一个他们在 OSX 开发中的 UIWindow 或 UINavigationController(rootViewController:) 匹配。然后我发现AppDelegate.swift文件中似乎没有didFinishLaunchingWithOptions这样的东西,而是只有一个applicationDidFinishLaunching(_ aNotification: Notification)。
问题:
我已经为我的两个视图控制器取消选中 Is Initial Controller 选项,并且它们都有故事板 ID(标识符?)和它们自己指定的类。
MainPageController
用户指南页面控制器
或者我也应该指定和识别我的窗口控制器?因为'我不知道如何处理窗口控制器..
为了在AppDelegate 中为我的应用程序设置初始视图控制器,我需要做什么?
附:这是我的界面视图的图片:
故事板界面
AppDelegate.swift
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
let storyboard = NSStoryboard(name: NSStoryboard.Name("Main"), bundle: Bundle.main)
var window = MainWindow().window
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) - > Bool {
return true
}
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
let firstLaunch = UserDefaults.isFirstLaunch()
let mainPageController = storyboard.instantiateController(withIdentifier: NSStoryboard.Name("MainPageController")) as? MainPageController
let userGuidePageController = storyboard.instantiateController(withIdentifier: NSStoryboard.Name("MainPageController")) as? UserGuidePageController
if !firstLaunch {
print("first run")
window?.contentViewController = userGuidePageController
} else {
print("not first run")
window?.contentViewController = mainPageController
}
}
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}
}
我试过这种方式,但效果不佳,我的代码有什么问题吗?
【问题讨论】:
-
Mac OS Cocoa 基于窗口,而不是视图控制器。只需决定首先显示哪个窗口。
-
@matt 好的,但具体如何?你能通过代码告诉我怎么做吗?
标签: macos cocoa swift4 viewcontroller xcode10