ios – Textfield inside alert not engaged on iPhone, however nice on iPad (SwiftUI)

0
1
ios – Textfield inside alert not engaged on iPhone, however nice on iPad (SwiftUI)


I’ve a easy Listing inside a NavigationStack. I positioned a button on the backside of the checklist so as to add an merchandise to the array that populate the checklist.

The button triggers an alert with a Textfield in it to supply the title for the brand new merchandise.

This works flawlessly on my iPad, on previews utilizing each iPad and iPhone, and on Simulator additionally utilizing each iPad and iPhone.

Nonetheless, the TextField someway doesn’t work correctly in my precise iPhone (working 18.5 – possibly that is the problem? Although I’m additionally utilizing 18.5 on iPad).

It would not work this fashion: after I press the “+ ingredient” button, the alert seems accurately. I’m able to enter textual content in it. However after urgent the “Add” button on alert dialog the closure would not run (I put breakpoints inside closure, and they’re by no means reached).

Thanks upfront for any concepts.

(If I take the TextField out, and have the Add button throughout the alert simply add an ingredient to the array, that works nice.)

Here is the code

import SwiftUI

struct IngredientsEditView: View {
    
    @AppStorage("elements") var elements = starterIngredients
    @AppStorage("ingredientsDone") var ingredientsDone: Bool = false
    @State non-public var addingIngredient = false
    @State non-public var newIngredient = ""
    
    var physique: some View {
        NavigationStack {
            Listing {
                Part {
                    ForEach ($elements, id: .self, editActions: .all) { $ingredient in
                        Textual content(ingredient)
                    }
                    .onDelete(carry out: delete)
                    .onMove(carry out: transfer)
                } 
                Button("+ ingredient") {
                    addingIngredient.toggle()
                }
                .alert("New Ingredient", isPresented: $addingIngredient) {
                    TextField("Ingredient", textual content: $newIngredient)
                    Button("Cancel", position: .cancel) {
                        newIngredient = ""
                    }
                    Button("Add") {
                        if !newIngredient.isEmpty {
                            elements.append(newIngredient)
                            newIngredient = ""
                        }
                    }
                }
            }
            .navigationTitle("Components")
            .toolbar {
                ToolbarItem(placement: .navigationBarTrailing) {
                    EditButton()
                }
                ToolbarItem(placement: .navigationBarLeading) {
                    Button("Begin Ordering!") {
                        ingredientsDone = true
                    }
                }
            }
        }
    }
    
    func delete(indexSet: IndexSet) {
        elements.take away(atOffsets: indexSet)
    }
    
    func transfer(from supply: IndexSet, to vacation spot: Int) {
        elements.transfer(fromOffsets: supply, toOffset: vacation spot)
    }
}


#Preview {
    IngredientsEditView()
}

LEAVE A REPLY

Please enter your comment!
Please enter your name here