SwiftUI macOS 命令(菜单栏)和视图 [英] SwiftUI macOS Commands (menu bar) and View
问题描述
我开始学习 SwiftUI 和 macOS 开发.我正在使用 SwiftUI 生命周期.如何从菜单栏中的聚焦窗口调用函数.
Hi I am starting to learn SwiftUI and macOS development. I am using the SwiftUI life cycle. How do I call a function from the focused window from the menu bar.
除了 Apple 文档之外,我还找到了此参考资料,并且是能够使用命令创建菜单项,但我不知道如何从我的视图中调用函数.
Besides Apple documentation, I found this reference and am able to create menu items using Commands but I have no idea how to call a function from my view.
例如:
假设这是我的应用程序结构:
Suppose this is my App struct:
import SwiftUI
@main
struct ExampleApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}.commands {
CommandMenu("First menu") {
Button("Action!") {
// How do I call the views action function?
}
}
}
}
这是我的观点:
struct ContentView: View {
public func action() {
print("It works")
}
var body: some View {
Text("Example")
}
}
如果有任何错别字,抱歉我只是输入了示例代码,但我希望你能理解.
I just typed the example code sorry if there are any typos but I hope you can get the idea.
推荐答案
因为 SwiftUI 中的 View
是瞬态的,你不能持有对 ContentView的特定实例的引用代码> 调用一个函数.不过,您可以做的是更改传递给内容视图的部分状态.
Because View
s in SwiftUI are transient, you can't hold a reference to a specific instance of ContentView
to call a function on it. What you can do, though, is change part of your state that gets passed down to the content view.
例如:
@main
struct ExampleApp: App {
@StateObject var appState = AppState()
var body: some Scene {
WindowGroup {
ContentView(appState: appState)
.frame(maxWidth: .infinity, maxHeight: .infinity)
}.commands {
CommandMenu("First menu") {
Button("Action!") {
appState.textToDisplay = "\(Date())"
}
}
}
}
}
class AppState : ObservableObject {
@Published var textToDisplay = "(not clicked yet)"
}
struct ContentView: View {
@ObservedObject var appState : AppState
var body: some View {
Text(appState.textToDisplay)
}
}
注意 .commands
修饰符继续 WindowGroup { }
Note that the .commands
modifier goes on WindowGroup { }
在这个例子中,AppState
是一个 ObservableObject
,它保存了应用程序的一些状态.它使用参数传递给 ContentView
.您还可以通过环境对象(https://www.hackingwithswift.com/quick-start/swiftui/how-to-use-environmentobject-to-share-data-between-views)
In this example, AppState
is an ObservableObject
that holds some state of the app. It's passed through to ContentView
using a parameter. You could also pass it via an Environment Object (https://www.hackingwithswift.com/quick-start/swiftui/how-to-use-environmentobject-to-share-data-between-views)
单击菜单项时,它会设置 textToDisplay
,它是 AppState
上的 @Published
属性.ContentView
将在 AppState
的 @Published 属性更新时更新.
When the menu item is clicked, it sets textToDisplay
which is a @Published
property on AppState
. ContentView
will get updated any time a @Published property of AppState
gets updated.
这是您将使用的模式的总体思路.如果您有此模式未涵盖的用例,请在评论中告诉我.
This is the general idea of the pattern you'd use. If you have a use case that isn't covered by this pattern, let me know in the comments.
更新,根据您的评论:
import SwiftUI
import Combine
@main
struct ExampleApp: App {
@StateObject var appState = AppState()
var body: some Scene {
WindowGroup {
ContentView(appState: appState)
.frame(maxWidth: .infinity, maxHeight: .infinity)
}.commands {
CommandMenu("First menu") {
Button("Action!") {
appState.textToDisplay = "\(Date())"
}
Button("Change background color") {
appState.contentBackgroundColor = Color.green
}
Button("Toggle view") {
appState.viewShown.toggle()
}
Button("CustomCopy") {
appState.customCopy.send()
}
}
}
}
}
class AppState : ObservableObject {
@Published var textToDisplay = "(not clicked yet)"
@Published var contentBackgroundColor = Color.clear
@Published var viewShown = true
var customCopy = PassthroughSubject<Void,Never>()
}
class ViewModel : ObservableObject {
@Published var text = "The text I have here"
var cancellable : AnyCancellable?
func connect(withAppState appState: AppState) {
cancellable = appState.customCopy.sink(receiveValue: { _ in
print("Do custom copy based on my state: \(self.text) or call a function")
})
}
}
struct ContentView: View {
@ObservedObject var appState : AppState
@State var text = "The text I have here"
@StateObject private var viewModel = ViewModel()
var body: some View {
VStack {
Text(appState.textToDisplay)
.background(appState.contentBackgroundColor)
if appState.viewShown {
Text("Shown?")
}
}
.onReceive(appState.$textToDisplay) { (newText) in
print("Got new text: \(newText)")
}
.onAppear {
viewModel.connect(withAppState: appState)
}
}
}
在我的更新中,您可以看到我已经解决了背景颜色的问题,显示隐藏视图,甚至在 @Published 属性之一时收到通知(通过 onReceive
)变化.
In my updates, you can see that I've addressed the question of the background color, showing hiding a view, and even getting a notification (via onReceive
) when one of the @Published properties changes.
您还可以看到我如何使用自定义发布者 (customCopy
) 将操作传递给 ContentView
的 ViewModel
You can also see how I use a custom publisher (customCopy
) to pass along an action to ContentView
's ViewModel
这篇关于SwiftUI macOS 命令(菜单栏)和视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!