0

我实现了获取双击以启动我的 macOS 应用程序(Swift 4.x)的文件名称的NSApplicationDelegate方法。func application(_ sender: NSApplication, openFile filename: String) -> Bool但是,似乎在调用此方法之后ViewController viewDidLoad(),我的所有初始化代码都发生了。有没有办法在viewDidLoad()类中获取文件名,ViewController以便我可以直接在初始化代码中使用文件名?

更新:

我现在可以根据此链接对 Swift 4.2 的建议将数据从 传递AppDelegate到:ViewController

https://stackoverflow.com/questions/15049924/passing-data-from-app-delegate-to-view-controllerhttps://stackoverflow.com/questions/15049924/passing-data-from-app-delegate-to-视图控制器

所以我的问题仍然是如何直接获取文件名,ViewDidLoad()或者能够通过获取名称application(_, sender: NSApplication, openFile filename: String) -> Bool并将其传递给ViewController何时ViewDidLoad()被调用。

4

1 回答 1

0

解决了我的问题。我没有在 中执行我的初始化代码ViewController,而是在 中实现了代码,AppDelegate applicationDidFinishLaunching以便可以在启动应用程序时和第一个事件发生之前执行初始化。我相信无论如何这是AppDelegate处理应用程序初始化的更合适的做法。ViewController此代码调整既解决了在and之间传递数据的需要,也解决了after 调用AppDelegate的问题。application(_, sender: NSApplication, openFile filename: String) -> BoolViewDidLoad()

于 2019-03-14T23:25:51.700 回答