在 swiftui 中将数据传递给 .popover
Passing data to .popover in swiftui
我有一个 String
值在按下按钮后会发生变化,我有一个 .popover
来显示它的值,但该值没有变化。我在每一步都添加了 print()
以清楚地表明该值已更改
我的ContentView.swift
import SwiftUI
struct ContentView: View {
@State private var showingPopover = false
@State private var popoverText = "hey, I'm the initial text"
var body: some View {
Button("press me!") {
print("1) \(popoverText)")
popoverText = "hey, I'm the new text"
print("2) \(popoverText)")
showingPopover = true
print("3) \(popoverText)")
}
.popover(isPresented: $showingPopover) {
Text(popoverText)
.onAppear(perform: {print("4) \(popoverText)")})
}
}
}
按下按钮后打印出来:
1) hey, I'm the initial text
2) hey, I'm the new text
3) hey, I'm the new text
4) hey, I'm the new text
并显示:
虽然我找到了一个奇怪的解决方法。如果我添加 Text(popoverText)
:
一切正常
import SwiftUI
struct ContentView: View {
@State private var showingPopover = false
@State private var popoverText = "hey, I'm the initial text"
var body: some View {
VStack{
Text(popoverText)
Button("press me!") {
print("1) \(popoverText)")
popoverText = "hey, I'm the new text"
print("2) \(popoverText)")
showingPopover = true
print("3) \(popoverText)")
}
}
.popover(isPresented: $showingPopover) {
Text(popoverText)
.onAppear(perform: {print("4) \(popoverText)")})
}
}
}
它打印同样的东西:
1) hey, I'm the initial text
2) hey, I'm the new text
3) hey, I'm the new text
4) hey, I'm the new text
但现在显示的是新文本:
我认为这与视图刷新或焦点有关,但我没有成功确定确切的事情
编辑:我的实际应用程序没有按钮,下面的代码只是作为示例,所以保持 .isPresented
是必要的,并且只允许使用 .isPresented
或 [=21] =]
.popover(item: someIdentifiableObj, content: ...) 用于将数据传递到 popover
struct HomeView: View {
@State var userObj: User?
var body: some View {
Button("press me!") {
userObj = User(popoverText: "New Text")
}
.popover(item: $userObj) { user in
Text(user.popoverText)
}
}
}
// MARK: - PREVIEW
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
HomeView()
}
}
struct User: Identifiable {
let id: UUID = UUID()
let popoverText: String
}
我有一个 String
值在按下按钮后会发生变化,我有一个 .popover
来显示它的值,但该值没有变化。我在每一步都添加了 print()
以清楚地表明该值已更改
我的ContentView.swift
import SwiftUI
struct ContentView: View {
@State private var showingPopover = false
@State private var popoverText = "hey, I'm the initial text"
var body: some View {
Button("press me!") {
print("1) \(popoverText)")
popoverText = "hey, I'm the new text"
print("2) \(popoverText)")
showingPopover = true
print("3) \(popoverText)")
}
.popover(isPresented: $showingPopover) {
Text(popoverText)
.onAppear(perform: {print("4) \(popoverText)")})
}
}
}
按下按钮后打印出来:
1) hey, I'm the initial text
2) hey, I'm the new text
3) hey, I'm the new text
4) hey, I'm the new text
并显示:
虽然我找到了一个奇怪的解决方法。如果我添加 Text(popoverText)
:
import SwiftUI
struct ContentView: View {
@State private var showingPopover = false
@State private var popoverText = "hey, I'm the initial text"
var body: some View {
VStack{
Text(popoverText)
Button("press me!") {
print("1) \(popoverText)")
popoverText = "hey, I'm the new text"
print("2) \(popoverText)")
showingPopover = true
print("3) \(popoverText)")
}
}
.popover(isPresented: $showingPopover) {
Text(popoverText)
.onAppear(perform: {print("4) \(popoverText)")})
}
}
}
它打印同样的东西:
1) hey, I'm the initial text
2) hey, I'm the new text
3) hey, I'm the new text
4) hey, I'm the new text
但现在显示的是新文本:
我认为这与视图刷新或焦点有关,但我没有成功确定确切的事情
编辑:我的实际应用程序没有按钮,下面的代码只是作为示例,所以保持 .isPresented
是必要的,并且只允许使用 .isPresented
或 [=21] =]
.popover(item: someIdentifiableObj, content: ...) 用于将数据传递到 popover
struct HomeView: View {
@State var userObj: User?
var body: some View {
Button("press me!") {
userObj = User(popoverText: "New Text")
}
.popover(item: $userObj) { user in
Text(user.popoverText)
}
}
}
// MARK: - PREVIEW
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
HomeView()
}
}
struct User: Identifiable {
let id: UUID = UUID()
let popoverText: String
}