0

I am trying to write a basic UI of a form to capture details about a restaurant. I will capture details like:

"NAME", placeHolder: "Fill in the restaurant name", "TYPE", placeHolder: "Fill in the restaurant type", "ADDRESS", placeHolder: "Fill in the restaurant address", "PHONE", placeHolder: "Fill in the restaurant phone", "DESCRIPTION", placeHolder: "Fill in the restaurant description",

I have designed the basic TextField model, stylised it and since I will have to reuse this style across all the entries in the form (as mentioned above) I decided to put the code in a Struct in a different SwiftUI file. Till here it is all fine and dandy now since the data is in a different file, I am getting struck at re-using this Struct data in ContentView. Any help or guidance will help me immensely.

Also, perhaps a silly question but is there any way to create a TextField without using a @State variable?

import Foundation
import SwiftUI

struct LabelTextField: View {

    @State private var restaurant: String = ""

    var label: String
    var placeHolder: String

    var body: some View {
     VStack(alignment: .leading){
         Text("NAME")
                .font(.headline)
                 TextField("Enter Restaurant Name", text: $restaurant)
                 .padding(.all)
                 .background(Color(red: 239.0/255.0, green: 243.0/255.0, blue: 244.0/255.0, opacity: 1.0))
                 .cornerRadius (5.0)
         } .padding(.horizontal, 15)
 }
}

1 Answer 1

3

You are almost there. You just need to pass the required variables to the LabelTextField. And TextField requires a Binding variable. I've updated your code to function as expected. Hope this helps. Happy coding.

struct ContentView: View {
    @State private var restaurant: String = ""

    var body: some View {
        VStack {
            LabelTextField(label: "NAME", placeHolder: "Fill in the restaurant name", restaurant: $restaurant)
            Text("Restaurant name - \(restaurant)")
        }
    }
}

struct LabelTextField: View {
    var label: String
    var placeHolder: String
    @Binding var restaurant: String

    var body: some View {
        VStack(alignment: .leading) {
            Text(label)
                .font(.headline)
            TextField(placeHolder, text: $restaurant)
                .padding(.all)
                .background(Color(red: 239.0/255.0, green: 243.0/255.0, blue: 244.0/255.0, opacity: 1.0))
                .cornerRadius (5.0)
        }
        .padding(.horizontal, 15)
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Did you check my code. I meant that it needs a state variable to be passed to it as a binding type.
I understand. I took those lines form the question posted. Updated the answer.
Thanks. I checked the code and it worked flawlessly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.