skip to Main Content

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


  1. you could try using Bindings when you call the SingleMember view, and use onSubmit{...} to check the value, such as:

     ForEach($memberInfo, id:.self){ $item in   // <-- $
         SingleMember(memberItem: $item)
      }
    

    where you declare

     struct SingleMember: View {
     @Binding var memberItem: MemberInfo
     // ...
     
     TextField("Amount", text: $amount)
         .onSubmit {
             if !amount.isEmpty {
                 memberItem.amount = ...
             }
         }
    

    or use memberItem.amount directly in the TextField, eg:

      TextField("Amount", value: $memberItem.amount, format: .number)
    
    Login or Signup to reply.
  2. If your data structure does not have amount in it but you still need to assign an amount for every item of your data instance then you should append the amount data to it one way or another.

    You could do with additional arrays but it might be much cleaner to simply wrap your value.

    struct MemberInfoWithAmount {
        let memberInfo: MemberInfo
        var amount: String
    }
    

    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:

    struct MemberInfoScreen: View {
        
        @State private var memberInfo: [MemberInfoWithAmount]
        
        init(memberInfo: [MemberInfo]) {
            self.memberInfo = memberInfo.map { .init(memberInfo: $0, amount: "") }
        }
        
        var body: some View {
            ForEach(memberInfo.indices, id: .self) { index in
                ZStack {
                    SingleMember(memberItem: memberInfo[index].memberInfo,
                                 amount: $memberInfo[index].amount)
                    if memberInfo[index].amount.isEmpty {
                        Text("This item is empty")
                    }
                }
            }
        }
    }
    
    extension MemberInfoScreen {
        
        private struct MemberInfoWithAmount {
            let memberInfo: MemberInfo
            var amount: String
        }
        
    }
    

    And yes, the amount is now a binding in your other view like so:

    struct SingleMember: View {
        
        var memberItem: MemberInfo
        @Binding 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)
                    
                }
            }
        }
        
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search