SwiftUI 对相同自定义警报操作的不同操作

SwiftUI different actions on the same Custom Alert actions

我创建了自定义提醒。我希望在第一个屏幕上单击“警报”上的“确定”按钮时将产品添加到购物篮中。当在第二个屏幕上按下 Ok 按钮时,请求购买产品。我在 2 个页面上调用了相同的警报,我希望它采取不同的操作。我不能用@Escaping 做到这一点。

AlertView

struct AlertView: View {
    @Binding var openShowAlert: Bool
    @State var closeShowAlert: Bool = false
    @State var openState: CGFloat = -UIScreen.main.bounds.height
    @State var closeState: CGFloat = UIScreen.main.bounds.height
    var title: String = ""
    var message: String = ""
    var okButtonText: String = ""
    var cancelButtonText: String = ""
    
   
    var body: some View {
        
        VStack {
            Text(title)
                .michromaFont(size: 20)
                .padding(.top)
            Spacer()
            Text(message)
                .michromaFont(size: 18)
            Spacer()
            HStack {
                Button(action: {
                    
                    self.openShowAlert = false
                    openState = -UIScreen.main.bounds.height
                    closeState = UIScreen.main.bounds.height
                    
                }) {
                    Text(cancelButtonText)
                        .foregroundColor(.red)
                }
                Spacer()
                
                Button(action: {}) {
                    Text(okButtonText)
                }
            }
            .michromaFont(size: 18)
            .padding([.horizontal, .bottom])
        }
        .neumorphisimBackground(width: 300, height: 200)
        .offset(y: self.openShowAlert ? self.openState : self.closeState)
        .animation(.easeInOut)
        
        .onChange(of: self.openShowAlert, perform: { value in
            if value {
                self.openState = .zero
            }
        })
    }
}

详细视图 在此屏幕上,单击警报演示以将产品添加到购物车。

struct DetailView: View {
    @Environment(\.presentationMode) var presentationMode
    var device = UIDevice.current.userInterfaceIdiom
    @State var width: CGFloat = 300
    @State var height: CGFloat = 450
    @Binding var text: String
    @State var showAlert: Bool = false

    var body: some View {

        ZStack() {
            ......
            AlertView(openShowAlert: self.$showAlert)
        }
        .navigationBarHidden(true)
        .navigationBarBackButtonHidden(true)
        
    }
}

CartView 单击我在此屏幕上提供购买产品的提醒。

struct CartView: View {
    @State var cartList = [1,2,3,4,5,6,7,8,9]
    @Environment(\.presentationMode) var presentationMode
    @State var showAlert: Bool = false
    var body: some View {
        ZStack(alignment: .top) {
            .....
            AlertView(openShowAlert: self.$showAlert)
        }
        .navigationBarHidden(true)
        .navigationBarBackButtonHidden(true)
    }
}

如何在同一警报中发送两个不同的操作。

嗯,我不明白为什么它不能与闭包一起使用。你试过像这样传递闭包吗?

struct AlertView: View {
    ...
    var okButtonAction: () -> ()

    var body: some View {
        ...
        Button(action: okButtonAction) {
            Text(okButtonText)
        }
    }   
}

用法

AlertView(openShowAlert: self.$showAlert) {
    // Your custom code 
}

备选方案

您可以使用 Combine 并创建一个具有特定密钥的发布者来识别发件人屏幕。然后你可以把你的自定义代码放在 .onReceive().

里面