如何从另一个视图(swiftUI)向数组追加数据?

uqdfh47h  于 2022-11-21  发布在  Swift
关注(0)|答案(1)|浏览(141)

在第一个视图中,我有一个文本字段和一个日期选择器。我如何将这些信息附加到另一个视图中的数组中,以便将输入的信息添加到列表中?我有以下结构:

import Foundation

struct Friend: Identifiable {
    let name: String
    let bday: String
    let id = UUID()
}

和此列表:

import SwiftUI

struct FriendView: View {
    
    @State private var friends = [Friend]()
    
    var body: some View {
        List(friends) { Friend in
            HStack {
                Text(Friend.name)
                Spacer()
                Text(Friend.bday)
            }
        }
    }
}

struct FriendView_Previews: PreviewProvider {
    static var previews: some View {
        FriendView()
    }
}

这是我想要将表单中的信息附加到friends数组的视图,但是我在addFriend函数中不断得到“'AddFriendView'类型的值没有成员'friends'”错误。

struct AddFriendView: View {
    @State var input: String = ""
    @State var date: Date = Date()
    
    

    var dateFormatter: DateFormatter {
        let formatter = DateFormatter()
        formatter.dateStyle = .medium
        return formatter
    }
    
    
    func addFriend () {
        self.friends.append(Friend(name: "name", bday: "birthday")) //ERROR POPS UP HERE
        dump(friends)
    }

    var body: some View {
        VStack {

            Spacer()
            
            TextField("Enter name", text: $input)
                .foregroundColor(.yellow)
                .padding(.all)
                .multilineTextAlignment(.center)
                .disableAutocorrection(true)
                .font(.largeTitle)
                .onTapGesture {
                  self.hideKeyboard()
                }
            
            Text("was born on a date")
                .font(.title)
                .foregroundColor(.yellow)
            
            DatePicker(
                "",
                selection: $date,
                displayedComponents: [.date]
            )
                .labelsHidden()
            
            Spacer()
            
            Button(
                "Save birthday"
            ) {
                self.addFriend()
                //print("\(self.input) \t \(self.dateFormatter.string(from: self.date))")
                self.input = ""
            }
                .padding(.all, 50)
                .accentColor(.white)
                .background(Color.yellow)
                .cornerRadius(25)
           // Spacer()
        }
    }
}

如有任何帮助,我将不胜感激。提前感谢您
我尝试添加@Binding var friends,但随后收到“模式中缺少Type annotation”错误,还收到@Binding var friends: [Friend],但随后在此处的末尾收到“调用中缺少参数'friends'的参数”错误:

struct AddFriendView_Previews: PreviewProvider {
    static var previews: some View {
        AddFriendView()
    }
}
i7uq4tfw

i7uq4tfw1#

@Binding var friends: [Friend]

是正确的。预览中缺少的参数可以通过提供一个常量、一个空数组

struct AddFriendView_Previews: PreviewProvider {
    static var previews: some View {
        AddFriendView(friends: .constant([]))
    }
}

或者一个朋友样本

struct AddFriendView_Previews: PreviewProvider {
    static var previews: some View {
        AddFriendView(friends: .constant([Friend(name: "John Doe", bday: "Jan 1, 2000")]))
    }
}

相关问题