SwiftUI:MacOS 上的 AppDelegate
我正在将 SwiftUI iOS 应用程序移植到 macOS。它使用 @UIApplicationDelegateAdaptor 属性包装器来绑定 UIApplicationDelegate 类。不幸的是,UIApplicationDelegate 类在 macOS 上不可用,所以我想知道如何绑定我的自定义 AppDelegate。
class AppDelegate: NSObject, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
...
}
}
struct MyApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
...
}
}
I'm porting a SwiftUI iOS app to macOS. It uses the @UIApplicationDelegateAdaptor property wrapper to bind a UIApplicationDelegate class. Unfortunately, the UIApplicationDelegate class isn't available on macOS, so I'm wondering how I bind my custom AppDelegate.
class AppDelegate: NSObject, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
...
}
}
struct MyApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
...
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
macOS 等效项具有 NS 前缀
,并且
DidFinishLaunching
委托方法具有不同的签名The macOS equivalent has the NS prefix
and the
DidFinishLaunching
delegate method has a different signature几乎是一样的,但是使用 NS 而不是 UI。我的做法与您略有不同:
然后您可以编写您的应用程序委托,如下所示:
这一切都对我有用。
it's almost the same, but then use NS instead of UI. I did it slightly different than you:
Then you can write your application delegate like:
This all worked for me.