【问题标题】:How is it possible to work with a struct inside of a struct in SwiftUI in different Views?如何在不同的视图中使用 SwiftUI 中的结构内部的结构?
【发布时间】:2020-01-21 14:03:55
【问题描述】:

我有一个简单的项目,在结构内部有一个结构,并且想首先添加单个用户的姓名和爱好,而不是想将此用户添加到整个用户池中。代码如下:

import SwiftUI

struct User: Identifiable, Hashable {
    var id = UUID()
    var firstName = ""
    var lastName = ""
    var hobbiesOfUser = [Hobbies]()
}

struct Hobbies: Identifiable, Hashable {
    var id = UUID()
    var nameOfHobby = ""
    var nameClub = ""
}

class UsersStorage: ObservableObject {
   @Published var users = [User]()
}

struct ContentView: View {

    @EnvironmentObject var userStorage: UsersStorage
    @State private var isPresented = false

    var body: some View {
        NavigationView {
            List(userStorage.users) { singleUser in
                VStack {
                    HStack {
                        Text(singleUser.firstName)
                         Text(singleUser.lastName)
                    }
                }
                }
            .navigationBarItems(trailing:
                    Button(action: {
                        self.isPresented = true
                    }) {
                        Text("New User")
                    }.sheet(isPresented: $isPresented, onDismiss: {
                        self.isPresented = false
                    }) {
                        AddUserView(isPresented: self.$isPresented, user: User()).environmentObject(self.userStorage)
                    }

                )
        }
    }
}

struct AddUserView: View {

@EnvironmentObject var userStorage: UsersStorage
@Binding var isPresented: Bool
@State var user: User
@State var hobbiesOfUser = [Hobbies]()



    var body: some View {
        NavigationView {
            VStack {
                Text("First Name")
                TextField("First Name", text: $user.firstName)
                Text("Last Name")
                TextField("Last Name", text: $user.lastName)
                NavigationLink(destination: AddHobbieView(hobbie: Hobbies())) {
                    Text("Add New Hobbie")
                }
                List(user.hobbiesOfUser) { singleHobbie in
                    VStack {
                        HStack {
                            Text(singleHobbie.nameOfHobby)
                             Text(singleHobbie.nameClub)
                        }
                    }
                }
                HStack {
                    Button(action: {
                        self.isPresented = false
                    }){
                        Text("Cancel")
                    }
                    Button(action: {
                        self.userStorage.users.append(self.user)
                        self.isPresented = false
                    }){
                        Text("Save")
                    }
                }

            }
        }
    }

}

struct AddHobbieView: View {

@EnvironmentObject var userStorage: UsersStorage
    @State var hobbie: Hobbies


    var body: some View {
        VStack {
            Text("Hobby")
            TextField("First Name", text: $hobbie.nameOfHobby)
            Text("Club")
            TextField("Last Name", text: $hobbie.nameClub)
            HStack {
                Button(action: {
//                    self.userStorage.users.append(self.hobbie)
                }){
                    Text("Cancel")
                }
                Button(action: {
                }){
                    Text("Save")
                }
            }

        }
    }

}

我的问题是:如何将爱好添加到 AddUserView 中的列表中并获取 AddHobbieView 中的按钮让我回到 AddUserView。

【问题讨论】:

    标签: list struct swiftui


    【解决方案1】:

    您将@Environment(\.presentationMode) var presentationMode 添加到AddHobbieView 并在您想关闭view 时调用self.presentationMode.wrappedValue.dismiss()

    我希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-28
      • 2015-02-02
      • 2020-08-22
      相关资源
      最近更新 更多