Home Alert with TextField SwiftUI
Post
Cancel

Alert with TextField SwiftUI

Alert in SwiftUI supports content through @ViewBuilder actions.

1
alert<S, A, M>(_ title: S, isPresented: Binding<Bool>, @ViewBuilder actions: () -> A, @ViewBuilder message: () -> M) -> some View where S : StringProtocol, A : View, M : View

It can have TextFields for user prompts. For example, you can add a TextFiled to let the user enter the name of the file:

1
2
3
4
5
.alert("Input Title!", isPresented: $showNameAlert) {
	TextField("Enter File Name", text: $fileName)
	Button("Ok", action: submitName)
 } message: {           
}

Define $showNameAlert & $fileName as two state variables. Also, define submitName method.

This post is licensed under CC BY 4.0 by the author.