Age*_*lly 5 macos cocoa viewcontroller swift4 xcode10
介绍:
我想创建一个在首次启动时显示一些“用户指南”的应用程序,我已经完成了检测该应用程序是否首次启动的部分,但是当涉及到设置视图控制器部分时,我无法确定怎么做。
现在的情况:
我已经进行了很多研究,并且看到了很多有关“如何以编程方式快速设置初始视图控制器”这样的主题的Q / A文章,但是基本上所有这些文章都是以ios开发为例的,它们都没有UIWindow或UINavigationController(rootViewController:)在OSX开发中匹配。然后我发现它似乎没有这样的事情didFinishLaunchingWithOptions中AppDelegate.swift的文件,相反,这里只有一个applicationDidFinishLaunching(_ aNotification: Notification)。
题:
我已经取消选中Is Initial Controller两个View Controller 的选项,并且它们都具有Storyboard ID(标识符?)和它们自己的指定类。
MainPageController

UserGuidePageController

还是我也应该指定并标识我的窗口控制器?因为我不知道该如何使用窗口控制器。
为了在其中设置应用程序的初始视图控制器,我需要做什么AppDelegate?
PS这是我的界面视图的图片:
故事板界面

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
}
}Run Code Online (Sandbox Code Playgroud)
我尝试过这种方式,但是效果不佳,我的代码有什么问题吗?
一开始你需要从 iOS 应用程序的应用程序委托类中处理这种情况
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let shouldShowUserGuide = // your cached key from user defaults for example
let mainController = //MainPageController Instance
let userGuide = //UserGuidePageController Instance
if shouldShowUserGuide {
window.rootViewController = userGuide
}else{
window.rootViewController = mainController
}
window.makeKeyAndVisisble()
return true
}
Run Code Online (Sandbox Code Playgroud)
// 这个代码我只是在 sublime 上写的,而不是从 xcode 上写的,所以你可能需要格式化它并修复任何语法问题
对于 OSX 主窗口:NSWindowController
func applicationDidFinishLaunching(_ aNotification: Notification) {
let storyboard = NSStoryboard(name: NSStoryboard.Name("Main"), bundle: Bundle.main)
var window = storyboard.instantiateController(withIdentifier: "MainWindow") as! NSWindowController
let firstLaunch = true//UserDefaults.isFirstLaunch()
let mainPageController = storyboard.instantiateController(withIdentifier: "MainController") as! NSViewController
let userGuidePageController = storyboard.instantiateController(withIdentifier: "UserGuideController") as! NSViewController
if firstLaunch {
print("first run")
window.contentViewController = userGuidePageController
} else {
print("not first run")
window.contentViewController = mainPageController
}
window.showWindow(self) // needed as long as your window controller has no static window content (No Content View Controller)
}
Run Code Online (Sandbox Code Playgroud)