在 SwiftUi 的 Alert 功能中显示 TextField

问题描述 投票:0回答:2

我是 SwiftUi 的新手,我正在尝试使用 TextField 和操作按钮显示警报消息,我想为此使用警报功能,但我无法在警报功能中添加 TextField,我在下面共享了代码,我该如何添加警报功能中的文本字段?谢谢..

.alert(isPresented:$showAlertDialog) {                                            
    Alert(
          title: Text("Please Enter sended E-mail Code"),
          message: Text("There is no undo"),
          primaryButton: .destructive(Text("Submit")) {
          print("Deleting...")
          },
          secondaryButton: .cancel()
          )
          }
swiftui alert
2个回答
3
投票

自 iOS 15 起,警报无法显示

TextField
。但您可以制作自定义 SwiftUI 警报。

struct TextFieldAlert: ViewModifier {
    @Binding var isPresented: Bool
    let title: String
    @Binding var text: String
    let placeholder: String
    let action: (String) -> Void
    func body(content: Content) -> some View {
        ZStack(alignment: .center) {
            content
                .disabled(isPresented)
            if isPresented {
                VStack {
                    Text(title).font(.headline).padding()
                    TextField(placeholder, text: $text).textFieldStyle(.roundedBorder).padding()
                    Divider()
                    HStack{
                        Spacer()
                        Button(role: .cancel) {
                            withAnimation {
                                isPresented.toggle()
                            }
                        } label: {
                            Text("Cancel")
                        }
                        Spacer()
                        Divider()
                        Spacer()
                        Button() {
                            action(text)
                            withAnimation {
                                isPresented.toggle()
                            }
                        } label: {
                            Text("Done")
                        }
                        Spacer()
                    }
                }
                .background(.background)
                .frame(width: 300, height: 200)
                .cornerRadius(20)
                .overlay {
                    RoundedRectangle(cornerRadius: 20)
                        .stroke(.quaternary, lineWidth: 1)
                }
            }
        }
    }
}

extension View {
    public func textFieldAlert(
        isPresented: Binding<Bool>,
        title: String,
        text: Binding<String>,
        placeholder: String = "",
        action: @escaping (String) -> Void
    ) -> some View {
        self.modifier(TextFieldAlert(isPresented: isPresented, title: title, text: text, placeholder: placeholder, action: action))
    }
}

在根视图上使用它。 (例如,请勿将其附加到其他内部元素的

Button
)。这将确保禁用呈现视图。 使用示例:

struct ContentView: View {
    @State var isAlertDisplayed = false
    @State var text = "Text to change"
    
    var body: some View {
        VStack {
            Text("Press the button to show the alert").multilineTextAlignment(.center)
            Text("Current value is: \(text)")
            Button("Change value") {
                withAnimation {
                    isAlertDisplayed = true
                }
            }
            .padding()
        }
        .textFieldAlert(isPresented: $isAlertDisplayed, title: "Some title", text: $text, placeholder: "Placeholder", action: { text in
            print(text)
        })
        .padding()
    }
}

0
投票

我认为你实际上可以 - 也许它在 iOS 16 中回归。这被证实对我有用:

.alert("Alert message...", isPresented: $isShowingAlert) {
    TextField("", text: $alertInput)
    Button("OK", action: {})
}
© www.soinside.com 2019 - 2024. All rights reserved.