Guidance Needed: TextField, Import UI & Other Errors

  • This is my first app and I'm very new to coding. I started out with enthusiasm until becoming overwhelmed with Swift coding language...

  • The app I'm trying to design is very simple and will go hand in hand with the work I do. I attached a screenshot of the UI of what I'm trying to achieve.

All I need the app to do is to let me input the appropriate information, and save it to the table.

  • The problem is that:
  1. Swift doesn't import source files like C#, whenever I create a new swift file and try to import the file called User.swift, "no file exists named User"

I've looked all over and the examples I'm seeing don't make sense, I dont understand why Xcode/Playground doesn't see it.

  1. Instead of using the UI, I'm writing out:

TextField(Name, text: ???) I've tried defining a variable in my User.swift file (I can't import which might be the problem). Even if I could import the User.swift file, I'm not even sure of what would go after the "text:____" spot - I've tried using the $name or changing it to "self.name" and there's always a problem

I realize this may be trivial to some of you but I appreciate your input.

  • What I'm writing in my User.swift file:
import SwiftUI

struct User {
    var name: String
    var phone: String
    var company: String
    var userid: String
    var clientid: String
    var errorid: String
    var notes: String
        
    init(name: String, phone: String, company: String, userid: String, clientid: String, errorid: String, notes: String) {
        self.name = name
        self.phone = phone
        self.company = company
        self.userid = userid
        self.clientid = clientid
        self.errorid = errorid
        self.notes = notes
        }
    }

here's an example of what I'm trying to make

Hello, firstly I also am relatively new to SwiftUI and am working on my first app. Secondly I don’t actually know the answer to your question and certainly don’t want to confuse you but wanted to show you another way to approach Form Filling. This way works well but if it doesn’t suit your needs I certainly won’t be offended. The code I have supplied is by no way complete but I have tried to give you the necessary chunks for you to hopefully get an idea. I will also attach a screenshot to show the resulting Form. I hope this helps in some way.


class Cards: ObservableObject {
    @Published var firstCardNumber = "1"
    @Published var firstCardWords = gameArray [1]
    @Published var firstCardLatitude = gameArray [2]
    @Published var firstCardLongitude = gameArray [3]
    @Published var firstCardFirstWordClue = gameArray [4]
    @Published var firstCardSecondWordClue = gameArray [5]
    @Published var firstCardThirdWordClue = gameArray [6]
    @Published var firstCardAnswerClue = ""
    @Published var firstCardFirstLetters = ""
    @Published var firstCardSecondLetters = ""
    @Published var firstCardThirdLetters = ""
    @Published var firstCardFirstWord = ""
    @Published var firstCardSecondWord = ""
    @Published var firstCardThirdWord = ""
    @Published var firstCardLettersResult = []
    @Published var firstCardWordClueResult = []
    @Published var secondCardNumber = "2"

etc.

    @State private var submit = false
    @State private var count = 0
    @StateObject var cards = Cards()
    @State var firstCards = (String)()
    @State var firstCardsJoin = (String)()
    @State var secondCards = (String)()
    @State var secondCardsJoin = (String)()

etc.

                VStack (alignment: .center){
                    Text("Game Form")
                        .font(.headline)
                        .bold()
                        .frame(maxWidth: .infinity, alignment: .center)
                        .onAppear(perform: {
                            count = 1
                            firstCardsJoined ()
                        })
               }
            Section {
                TextField("Card Number:", text: $cards.firstCardNumber)
                    .multilineTextAlignment(.center)
                SecureField("full-stops.between.words", text: $cards.firstCardWords)
                    .textCase(.lowercase)
                    .keyboardType(.alphabet)
                    .disableAutocorrection(true)
                    .textInputAutocapitalization(.never)
                    .multilineTextAlignment(.center)
                TextField("Latitude (North-South)", text: $cards.firstCardLatitude)
                    .multilineTextAlignment(.center)
                TextField("Longitude (East-West)", text: $cards.firstCardLongitude)
                    .multilineTextAlignment(.center)
                TextField("First Word Clue. Maximum Charaters : 80", text: $cards.firstCardFirstWordClue)
                    .multilineTextAlignment(.center)
                TextField("Second Word Clue. Maximum Charaters : 80", text: $cards.firstCardSecondWordClue)
                    .multilineTextAlignment(.center)
                TextField("Third Word Clue. Maximum Charaters : 80", text: $cards.firstCardThirdWordClue)
                    .multilineTextAlignment(.center)
            }
            Section {
                TextField("Card Number:", text: $cards.secondCardNumber)
                    .multilineTextAlignment(.center)
                SecureField("full-stops.between.words", text: $cards.secondCardWords)
                    .textCase(.lowercase)
                    .keyboardType(.alphabet)
                    .disableAutocorrection(true)
                    .textInputAutocapitalization(.never)
                    .multilineTextAlignment(.center)
                TextField("Latitude (North-South)", text: $cards.secondCardLatitude)
                    .multilineTextAlignment(.center)
                TextField("Longitude (East-West)", text: $cards.secondCardLongitude)
                    .multilineTextAlignment(.center)
                TextField("First Word Clue. Maximum Charaters : 80", text: $cards.secondCardFirstWordClue)
                    .multilineTextAlignment(.center)
                TextField("Second Word Clue Maximum Charaters : 80", text: $cards.secondCardSecondWordClue)
                    .multilineTextAlignment(.center)
                TextField("Third Word Clue Maximum Charaters : 80", text: $cards.secondCardThirdWordClue)
                    .multilineTextAlignment(.center)
            }

etc.

                self.submit.toggle()
                if count == 1 {
                    firstCardsJoined()
                }
            }) {
                Text("✅  Okay  ✅")
                    .font(.headline)
                    .foregroundColor(.red)
                    .frame(maxWidth: .infinity, alignment: .center)
                    .padding()
            }.sheet(isPresented: $submit) {
                FormResultView(cards: cards)
            }
        }
    }
    func firstCardsJoined () {
        firstCardsJoin =
        cards.firstCardNumber + "," + cards.firstCardWords + "," +
        cards.firstCardLatitude + "," + cards.firstCardLongitude + "," +
        cards.firstCardFirstWordClue + "," + cards.firstCardSecondWordClue + "," +
        cards.firstCardThirdWordClue + ",,,,"
        firstCardsLettersJoined()
    }
    func firstCardsLettersJoined () {
        firstCards = firstCardsJoin
        secondCardsJoined()
    }
    func secondCardsJoined () {
        secondCardsJoin =
        cards.secondCardNumber + "," + cards.secondCardWords + "," +
        cards.secondCardLatitude + "," + cards.secondCardLongitude + "," +
        cards.secondCardFirstWordClue + "," + cards.secondCardSecondWordClue + "," +
        cards.secondCardThirdWordClue + ",,,,"
        secondCardsLettersJoined()
    }
    func secondCardsLettersJoined () {
        secondCards = secondCardsJoin
        thirdCardsJoined()
    }

etc.

        cards.sharedFile = firstCards + "," + secondCards + "," + thirdCards + "," + fourthCards + "," + fifthCards + "," + sixthCards
    }

Array Builder

    @ObservedObject var cards = Cards()
    @State var firstCards = (String)()
    @State var firstCardsJoin = (String)()
    @State var firstCardLetters = (String)()
    @State var secondCards = (String)()
etc.
    func sixthCardsLettersJoined () {
        sixthCardLetters =
        cards.sixthCardFirstLetters + "," +  cards.sixthCardSecondLetters + "," +
     etc.
    func allCardsJoined () {
        formData = firstCards + "," + secondCards + "," + thirdCards + "," + fourthCards + "," + fifthCards + "," + sixthCards
        formArray = formData.components(separatedBy: ",")
        gameArray = formArray
    }

Part of the Form

A Resulting Card / Record

That's a really helpful example and I appreciate it. I will most certainly save this.

I included two screenshots to show you what I mean, I'm hoping they illustrate what I'm trying to do. Seems like you're further along than I am.

  1. how to import the "user" page into the ContentView
  2. when using the TextField("Placeholder", text: ????) - what should I put where the ???? are? apple says

Hello again

I hope this explanation helps you better (My explanations of the Code may not be using the correct terminology but they are how I understand them (Layman’s Terms)).

    @Published var firstCardNumber = "1" // Form Inputs need to be made Observable (Cards:)

|

struct FormBuilder: View {
    @StateObject var cards = Cards() // Attaching variables in Form to an ObservableObject (Cards:)
|

TextField("Card Number:", text: $cards.firstCardNumber) // Creating field in the Form and attaching to Cards:

|

Text("✅  Okay  ✅")
                    .font(.headline)
                    .foregroundColor(.red)
                    .frame(maxWidth: .infinity, alignment: .center)
                    .padding()
            }.sheet(isPresented: $submit) {
                FormResultView(cards: cards) // Passing Form Data to another View (Form Result View)
            }
|

struct FormResultView: View {
    
    @ObservedObject var cards = Cards() // Making the Form Inputs able to be seen in this View

    var body: some View {

|

Section {
                     VStack {

         Text("Card Number: \(cards.firstCardNumber)") // The Form Field Input appears here
Guidance Needed: TextField, Import UI & Other Errors
 
 
Q