简体   繁体   中英

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.

I've recently been using Swift, and I haven't found anything to fix it. How can I solve?

You have two different AdministratorManager instances. One in UserList , second in userInfo . They are not connected and have different data.

Change to this in userInfo

@EnvironmentObject var administratorManager: AdministratorManager

And in UserList pass environment object to next screen

Group {   
  userInfo()  
  .environmentObject(administratorManager)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM