Keyboard not entering in Textfield in SwiftUI

Hello,

Whenever I enter text from my keyboard into a textfield, it either doesn't register or it only remembers the last character. The textfield is rendered completely unusable, even when I load it up on my phone. Any advice? Is this a weird bug in my code, or is this an Xcode problem?

There is probably an error in your use of TextField. Please show code so that we can tell.

Here's my code:



struct NewCharacterSheet: View {
    @State private var newCharacter = Entity.emptyEntity
    @Binding var cardList: [Entity]
    @Binding var isPresentingNewCharacterView: Bool
    
    var body: some View {
        NavigationStack {
            CharacterEditView(character: $newCharacter)
                .toolbar {
                    ToolbarItem(placement: .cancellationAction) {
                        Button("Dismiss") {
                            isPresentingNewCharacterView = false
                        }
                    }
                    ToolbarItem(placement: .confirmationAction) {
                        Button("Add") {
                            cardList.append(newCharacter)
                            isPresentingNewCharacterView = false
                        }
                    }
                }
        }
    }
}


struct NewCharacterSheet_Previews: PreviewProvider {
    static var previews: some View {
        NewCharacterSheet(cardList: .constant(Entity.sampleData), isPresentingNewCharacterView: .constant(true))
    }
}

stopped when I added a feature somewhere else in the code

What did you add ? Where ?

I do not see any TextField in the code you posted. So impossible to say anything.

Oops, sorry! Busy couple days.

//
//  CharacterEditView.swift
//  CPR Health Tracker
//
//  View for creating or editing a character
//  Originally based off npcCreationView.swift

import SwiftUI

struct CharacterEditView: View {
    @Binding var character: Entity
    
    // number display width constant
    static let widthConstant: Double = 25
    
    var body: some View {
        Form {
            Section(header: Text("Character Edit")) {
                VStack {
                    HStack {
                        Text("Name:")
                        TextField("Character Name", text: $character.name)
                    }
                    
                    // initiative hstack
                    HStack {
                        Image(systemName: "figure.run")
                            .foregroundColor(.orange)
                        Text("Initiative:")
                        TextField("#", text: $character.initiativeInString)
                    }
                    HStack {
                        Image(systemName: "shield.fill")
                            .foregroundColor(.green)
                        VStack {
                            HStack {
                                Text("Head SP:")
                                TextField("Head SP", text: $character.headArmorInString)
                            }
                            HStack {
                                Text("Body SP:")
                                TextField("Body SP", text: $character.bodyArmorInString)
                            }
                        }
                    }
                    
                    // health hstack
//                    HStack {
//                        Slider(value: $character.healthInDouble, in: 0...50, step: 5) {
//                            Text("Health")
//                        }
//                        Spacer()
//                        Image(systemName: "heart.fill")
//                            .foregroundColor(.red)
//                        TextField("#", text: $character.healthInString)
//                            .frame(width: CharacterEditView.widthConstant)
//                        Text("HP")
//                    }
                    HStack {
                        Image(systemName: "heart.fill")
                            .foregroundColor(.red)
                        Text("HP:")
                        TextField("HP", text: $character.healthInString)
                    }
                }
            }
        }
    }
}

struct CharacterEditView_Preview: PreviewProvider {
    static var previews: some View {
        CharacterEditView(character: .constant(Entity.sampleData[0]))
    }
}

//
//  ListView.swift
//  CPR Health Tracker
//
// Displays the list of entity cards

import SwiftUI

struct ListView: View {
    @State var cardList: [Entity]
    @State private var isPresentingNewCharacterView = false
    
    var body: some View {
        NavigationStack {
            List($cardList) { $i in
                NavigationLink(destination: DetailView(character: $i, editingChar: Entity.emptyEntity, cardList: $cardList)) {
                    CardView(character: i)
                }
            }
            .navigationTitle("Characters")
            .toolbar {
                Button(action: {
                    isPresentingNewCharacterView = true
                } ) {
                    Image(systemName: "plus")
                }
            }
        }
        .sheet(isPresented: $isPresentingNewCharacterView) {
            NewCharacterSheet(cardList: $cardList, isPresentingNewCharacterView: $isPresentingNewCharacterView)
        }
// ERROR BELOW
        .onChange(of: cardList) { newList in
            cardList = newList.sorted(by: >)
        }
        .onAppear {
            cardList.sort(by: >)
        }
// ERROR ABOVE
    }
}

struct ListView_Preview: PreviewProvider {
    static var previews: some View {
        ListView(cardList: Entity.sampleData)
    }
}
//
//  Entity.swift
//  CPR Health Tracker
//
// Represents either an Entity or PC
// Originally based off the same code as npc.swift

import Foundation

struct Entity: Identifiable {
    let id: UUID
    var name: String
    var initiative: Int
    var initiativeInString: String {
        get {
            String(initiative)
        }
        set {
            initiative = Int(newValue) ?? 0
        }
    }
    var bodyArmor: Int
    var bodyArmorInDouble: Double {
        get {
            Double(bodyArmor)
        }
        set {
            bodyArmor = Int(newValue)
        }
    }
    var bodyArmorInString: String {
        get {
            String(bodyArmor)
        }
        set {
            bodyArmor = Int(newValue) ?? 0
        }
    }
    var headArmor: Int
    var headArmorInDouble: Double {
        get {
            Double(headArmor)
        }
        set {
            headArmor = Int(newValue)
        }
    }
    var headArmorInString: String {
        get {
            String(headArmor)
        }
        set {
            headArmor = Int(newValue) ?? 0
        }
    }
    var health: Int
    var healthInDouble: Double {
        get {
            Double(health)
        }
        set {
            health = Int(newValue)
        }
    }
    var healthInString: String {
        get {
            String(health)
        }
        set {
            health = Int(newValue) ?? 0
        }
    }
    
    init(id: UUID = UUID(), name: String, initiative: Int, bodyArmor: Int, headArmor: Int, health: Int) {
        self.id = id
        self.name = name
        self.initiative = initiative
        self.bodyArmor = bodyArmor
        self.headArmor = headArmor
        self.health = health
    }
}

// empty Entity
extension Entity {
    static var emptyEntity: Entity {
        Entity(name: "", initiative: 0, bodyArmor: 0, headArmor: 0, health: 0)
    }
}

// sample entities
extension Entity {
    static var sampleData: [Entity] {
        [
            Entity(name: "Easy Goon", initiative: 4, bodyArmor: 4, headArmor: 0, health: 15),
            Entity(name: "Average Goon", initiative: 6, bodyArmor: 7, headArmor: 4, health: 25),
            Entity(name: "Elite Goon", initiative: 8, bodyArmor: 11, headArmor: 11, health: 35)
        ]
    }
}

// comparable extension
// ERROR BELOW
extension Entity:Comparable {
    static func == (lhs: Entity, rhs: Entity) -> Bool {
        return lhs.initiative == rhs.initiative
    }
    
    static func < (lhs: Entity, rhs: Entity) -> Bool {
        return lhs.initiative < rhs.initiative
    }
}
// ERROR ABOVE

It seems the comparable extension causes the weird textfield problems. The stuff I added are labeled with ERROR BELOW.

Wait, I just figured it out. I need to put each of the variables from Entity in the comparable function for TextFields to continue to work. Like so:

// comparable extension
extension Entity:Comparable {
    static func == (lhs: Entity, rhs: Entity) -> Bool {
        return lhs.initiative == rhs.initiative &&
        lhs.name == rhs.name
    }
    
    static func < (lhs: Entity, rhs: Entity) -> Bool {
        return lhs.initiative < rhs.initiative &&
        lhs.name < rhs.name
    }
}
Keyboard not entering in Textfield in SwiftUI
 
 
Q