How can I check the amount from singleMember is it empty, I need to have instances of this:
struct SingleMember: View {
var memberItem: MemberInfo
@State var amount: String = ""
var body: some View {
VStack(alignment:.leading){
Divider()
HStack{
Image("avatar")
VStack(alignment:.leading){
Text("(memberItem.firstName ?? "Unknown")")
.font(.custom("Ubuntu-Regular", size:15))
Text("(memberItem.phoneNumber ?? "Unknown")")
.font(.custom("Ubuntu-Regular", size:12))
.foregroundColor(.gray)
}
Spacer()
HStack{
TextField("Amount", text: $amount)
.frame(width:50)
.background(.clear)
Image(systemName: "square.and.pencil")
}.font(.custom("Ubuntu-Regular", size: 12))
.foregroundColor(Color.gray)
}
}
}
}
On Another View I have :
ForEach(memberInfo, id:.self){ item in
SingleMember(memberItem: item)
}
What I want is to check, if SingleItem amount is not empty, and if not to add to array item.id, item.amount, for example.
I had an idea of using @Binding on SingleMember but that doesn’t work because that update all TextFields, but I want every instance to have it’s own amount.
2
Answers
you could try using Bindings when you call the
SingleMember
view, and useonSubmit{...}
to check the value, such as:where you declare
or use
memberItem.amount
directly in theTextField
, eg:If your data structure does not have
amount
in it but you still need to assign anamount
for every item of your data instance then you should append theamount
data to it one way or another.You could do with additional arrays but it might be much cleaner to simply wrap your value.
You don’t want to change your view so you can create a custom constructor for it. You didn’t provide the code but the one with
ForEach
would look something like this:And yes, the amount is now a binding in your other view like so: