简体   繁体   English

通知视图在 swiftUI 中单击警报 OK 按钮

[英]Inform view that alert OK button is clicked in swiftUI

I have created a ViewModifier to show alert with 2 buttons.我创建了一个ViewModifier来显示带有 2 个按钮的警报。 How to inform my ContentView that OK button is clicked, so that I can perform button action?如何通知我的 ContentView 单击确定按钮,以便我可以执行按钮操作?

Sample code: ShowAlert is my custom ViewModifier示例代码:ShowAlert 是我的自定义 ViewModifier

struct ShowAlert: ViewModifier {

    @Binding var showingAlert: Bool
    let title: String
    let message: String    

    func body(content: Content) -> some View {
        content
            .alert(isPresented: $showingAlert) { () -> Alert in
                Alert(title: Text(title), message: Text(message),
                    primaryButton: .default (Text("OK")) {
                      print("OK button tapped") 
                      //How to trigger ok button clicked event to my content view
                },secondaryButton: .cancel())
        }
    }
}

View Implementation查看实施

       ScrollView {
                ....
            }.navigationBarTitle("Click")
            .navigationBarItems(trailing: Button(action: {
                self.showAlert = true
            }) {
                Image(systemName: "lock")
            }.modifier(ShowAlert(showingAlert: $showAlert, title: "", message: "Are you sure you want to Logout")) 

Here is a demo of solution with passed callback into modifier.这是一个将回调传递给修饰符的解决方案演示。 Tested with Xcode 11.4 / iOS 13.4.使用 Xcode 11.4 / iOS 13.4 进行测试。

演示

struct ShowAlert: ViewModifier {

    @Binding var showingAlert: Bool
    let title: String
    let message: String
    var callback: () -> () = {}    // << here !!

    func body(content: Content) -> some View {
        content
            .alert(isPresented: $showingAlert) { () -> Alert in
                Alert(title: Text(title), message: Text(message),
                    primaryButton: .default (Text("OK")) {
                      print("OK button tapped")
                      self.callback()             // << here !!
                },secondaryButton: .cancel())
        }
    }
}

// Demo view
struct TestAlertModifier: View {
    @State private var showAlert = false
    @State private var demoLog = "Wait for alert..."
    var body: some View {
        NavigationView {
            ScrollView {
                    Text(demoLog)
                }.navigationBarTitle("Click")
                .navigationBarItems(trailing: Button(action: {
                    self.showAlert = true
                }) {
                    Image(systemName: "lock")
                }.modifier(ShowAlert(showingAlert: $showAlert, title: "",
                                 message: "Are you sure you want to Logout", callback: confirmAlert)))
        }
    }

    private func confirmAlert() {
        self.demoLog = "Tapped - OK"
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UITEXTFIELD输入数据到警报框,单击确定 - UITEXTFIELD input data to alert box ok clicked 翻转回到同一视图控制器上的另一个视图通过在Swift 3中单击Alert Ok按钮 - Flip Back to Another View on the same view controller By click the Alert Ok Button in Swift 3 如何在视图的rest中设置一个视图,并在SwiftUI中点击一个按钮时立即改变视图? - How to set a view in the rest of the view and change the view immediately when a button is clicked in SwiftUI? 仅在用户点击警报视图中的按钮后才移动到其他选取器视图选项卡 - SwiftUI - Move to other Picker View tabs only after the user taps on a button in an alert view - SwiftUI 单击“确定”警报按钮后,无需使用segue即可导航到另一个视图 - Navigate to another View after OK Alert Button click without using segue 如何显示来自AppDelegate的一个视图控制器类的UIAlertView,并在单击警报按钮时如何在索引方法中击中单击的按钮? - How to show UIAlertView of one view controller class from AppDelegate and to hit clicked button at index method if alert button clicked? CBCentralManagerOptionShowPowerAlertKey AlertView /警报控制器“确定”按钮 - CBCentralManagerOptionShowPowerAlertKey AlertView / Alert Controller OK Button SwiftUI - 没有标题的操作表,Alert.Button 使用视图而不是文本 - SwiftUI - Action sheet with no title, Alert.Button using View instead of Text 如果出现警报/工作表,则制作 SwiftUI 视图灰度 - Make SwiftUI view grayscale if an alert/sheet is presented 在 SwiftUI 中为 Alert 组件设置文本和按钮颜色? - Setting text and button color for Alert component in SwiftUI?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM