[英]Why does the value change between views? SwiftUi
I am creating an app in which there is a list of users, with an associated button for each user that leads to another screen.我正在创建一个应用程序,其中有一个用户列表,每个用户都有一个关联的按钮,可以通向另一个屏幕。 This screen should display all the data of the selected user.此屏幕应显示所选用户的所有数据。
struct UserList: View {
@StateObject var administratorManager = AdministratorManager()
@State var ricerca = ""
@State var isPresented: Bool = false
var body: some View {
ScrollView(.vertical, showsIndicators: false) {
ForEach(administratorManager.users.sorted(using: [
KeyPathComparator(\.surname, order: .forward)
])) { user in
if user.toCheck.contains(where: {$0.range(of: ricerca.lowercased()) != nil}) || user.toCheck.contains(where: {$0.range(of: ricerca.capitalized) != nil}) || ricerca.isEmpty {
VStack(alignment: .leading) {
Text(user.number).font(.subheadline)
Text(user.name).font(.subheadline)
Text(user.surname).font(.subheadline)
}.background(Color("Purple"))
.cornerRadius(5)
// Button to change screen ---------------------------------------------------------------------------------------------------
Button {
isPresented.toggle()
administratorManager.infoUtente[2] = user.number
} label: {
Text("Tap me!")
.padding()
.background(Color.white)
.cornerRadius(20)
}.padding(1)
// ---------------------------------------------------------------------------------------------------------------------------
}
}.navigationBarTitle("User List")
.searchable(text: $ricerca, prompt: "Search")
.onAppear() {
administratorManager.fetchData(collection: "Users")
administratorManager.checkPermission()
}
.alert(isPresented: $administratorManager.isMessage) {
Alert(title: Text(administratorManager.title), message: Text(administratorManager.message),
dismissButton: .default(Text("Ho capito!")))
}
.sheetWithDetents(
isPresented: $isPresented,
detents: [.medium(),.large()]
) {
print("The sheet has been dismissed")
} content: {
Group {
userInfo()
}
.font(.title)
}
}
}
}
This is the screen where the user information will be displayed:这是显示用户信息的屏幕:
struct userInfo: View {
// Oggetti.
@StateObject var administratorManager = AdministratorManager() // Oggetto riguardante i comandi riservati agli admin.
var body: some View {
HStack {
Text("Nome: \(administratorManager.infoUtente[0])")
.padding(.horizontal, 5)
Text("Cogome: \(administratorManager.infoUtente[1])")
.padding(.horizontal, 5)
}.padding(1)
Text("Numero: \(administratorManager.infoUtente[2])")
.padding(.horizontal, 5)
Button("print", action: {
print(administratorManager.infoUtente[2])
}).padding(1)
}
}
If I print in the console the value of administratorManager.infoUser [2] is empty.如果我在控制台中打印,administratorManager.infoUser [2] 的值为空。
I've recently been using Swift, and I haven't found anything to fix it.我最近一直在使用 Swift,但我还没有找到任何可以解决的问题。 How can I solve?我该如何解决?
You have two different AdministratorManager
instances.您有两个不同的AdministratorManager
实例。 One in UserList
, second in userInfo
.一个在UserList
中,第二个在userInfo
中。 They are not connected and have different data.它们没有连接并且具有不同的数据。
Change to this in userInfo
在userInfo
中更改为这个
@EnvironmentObject var administratorManager: AdministratorManager
And in UserList
pass environment object to next screen并在UserList
传递环境 object 到下一个屏幕
Group {
userInfo()
.environmentObject(administratorManager)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.