当我在文本编辑器中按键盘上的空格时,应用程序控制台中会打印“父按钮单击”,但空格不会放入文本编辑器中。其他角色不存在这样的问题。在这种情况下如何在 TextEditor 中获取空间?
import SwiftUI
struct ContentView: View {
@State private var showPopoverComment = false
var body: some View {
Button(action: {
print("parent button click")
}) {
VStack {
Button("child button", action: {showPopoverComment = true})
.popover(isPresented: $showPopoverComment) {
EditCommentView()
.frame(width: 100, height: 100)
}
// other views ...
}
}.buttonStyle(.plain)
}
}
struct EditCommentView: View {
@State var comment: String = ""
var body: some View {
TextEditor(text: $comment)
}
}
将
.popover
移到父按钮之外。
struct ContentView: View {
@State private var showPopoverComment = false
var body: some View {
Button(action: {
print("parent button click")
}) {
VStack {
Button("child button", action: {showPopoverComment = true})
// other views ...
}
}.buttonStyle(.plain)
.popover(isPresented: $showPopoverComment) { // <--- here
EditCommentView()
.frame(width: 100, height: 100)
}
}
}