我正在將 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 {
...
}
}
uj5u.com熱心網友回復:
macOS 等效項具有 NS 前綴
import AppKit
class AppDelegate: NSObject, NSApplicationDelegate {
...
@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
并且DidFinishLaunching委托方法具有不同的簽名
func applicationDidFinishLaunching(_ aNotification: Notification) [ ...
uj5u.com熱心網友回復:
它幾乎相同,但隨后使用 NS 而不是 UI。我做的和你略有不同:
@main
struct MyApp: App {
// MARK: - Properties
// Used variables
@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
@Environment(\.openURL) var openURL
var body: some Scene {
WindowGroup {
MainControlView()
}
.commands {
FileMenuCommands(listOfContainers: listOfContainers)
}
}
}
然后你可以撰寫你的應用程式委托,如:
import AppKit
import SwiftUI
class AppDelegate: NSObject, NSApplicationDelegate {
// Whatever you want to write here
}
這一切都對我有用。
親切的問候, MacUserT
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/434542.html
上一篇:GUI猜數字游戲,直接開玩...
