What I want to achieve: when clicking the save button, it pops up a box asking for the name. after user gives a name and hits the confirm button, it performs some actions and dismisses the view (return to the parent view).
here’s my code, however, it doesn’t perform any actions when I save and confirm the first time. All following attempts works. any idea why it doesn’t work for the first time?
Button(action: {
self.isShowingNameInput = true
}) {
Text("SAVE")
}
.alert("Task Name", isPresented: self.$isShowingNameInput, actions: {
TextField("", text: self.$options.name)
Button(action: {
log("Saved (self.options.name)")
self.presentationMode.wrappedValue.dismiss()
}) {
Text("Confirm")
}.disabled(self.options.name.isEmpty)
Button(role: .cancel, action: {}) {
Text("Cancel")
}
}, message: {})
2
Answers
I am not sure if you can have a
TextField
in theactions
of an alert. In general, the data for an alert should not change after presentation occurs and this also applies to the values of state variables that you show in the content of the alert. See Update text inside alert message which relates to a similar issue.As a workaround, you could consider showing a sheet instead:
Alternatively, you could implement a custom alert. The answer to the post mentioned above shows an example of how this can be done (it was my answer).
Here is my test code that shows that the popup alert
button action, works every time. As can be seen, there is no problem having a
TextField
in the alert.On MacOS 14.5, using Xcode 15.4, tested on real iOS 17 devices and macCatalyst.