gpt4 book ai didi

macos - 如何使用 Swift 4 Xcode 10 从 Cocoa 中的 Storyboard 以编程方式设置初始 Controller ?

转载 作者:行者123 更新时间:2023-12-03 16:11:24 28 4
gpt4 key购买 nike

简介:

我想创建一个在首次启动时显示一些“用户指南”的应用程序,我已经完成了检测应用程序是否首次启动的部分,但是当涉及到设置 View Controller 部分时,我可以不知道该怎么做。

当前情况:

我做了很多研究,也看过很多关于“如何在 swift 中以编程方式设置初始 View Controller ”等主题的 Q/A 文章,但基本上都是以 ios 开发为例,没有一个它们的 UIWindowUINavigationController(rootViewController:) 与 OSX 开发中的匹配。然后我发现AppDelegate.swift文件中似乎没有didFinishLaunchingWithOptions这样的东西,相反,只有一个applicationDidFinishLaunching(_ aNotification: Notification )

问题:

我已经取消选中两个 View Controller 的Is Initial Controller选项,并且它们都有 Storyboard ID(标识符?)和它们自己指定的类。

MainPageController

UserGuidePageController

或者我也应该指定和识别我的窗口 Controller 吗?因为我不知道如何使用窗口 Controller ..

我需要做什么才能在 AppDelegate 中为我的应用程序设置初始 View Controller ?

附注这是我的界面 View 的图片:

Storyboard界面

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
}
}

我尝试了这种方法,但效果不太好,我的代码有什么问题吗?

最佳答案

一开始你需要从应用程序委托(delegate)类处理这种情况适用于 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
}

//这段代码是我在 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)
}

关于macos - 如何使用 Swift 4 Xcode 10 从 Cocoa 中的 Storyboard 以编程方式设置初始 Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52626056/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com