ios SwiftUI -显示和隐藏自定义警报

vfwfrxfs  于 2023-02-10  发布在  iOS
关注(0)|答案(1)|浏览(197)

我正在创建一个自定义警报可重用的修改器,将有不同数量的按钮取决于我需要的风格。无论如何,在我下面的示例代码,我已经删除了尽可能多的额外代码,我可以保持它的小和简单。
使用下面的代码,我可以显示警报,但单击"单击我"按钮不会消除警报。我觉得设置"isPresented"绑定变量的方式有问题,但无法找出SwiftUI的新特性。

自定义警报修饰符:

struct CustomAlertView: ViewModifier {
    var isPresented: Binding<Bool>

    init(isPresented: Binding<Bool>) {
        self.isPresented = isPresented
    }

    func body(content: Content) -> some View {
        content.overlay(alertContent())
    }

    @ViewBuilder
    private func alertContent() -> some View {
        GeometryReader { geometry in
            if self.isPresented {
                // Do something

                // Setting isPresented to false is not doing anything when button is clicked.
                Button(action: { self.isPresented.wrappedValue = false }) { 
                    Text("Click Me")
                }
            }
        }
    }
}

func customAlert(isPresented: Binding<Bool>) -> some View {
    return modifier(CustomAlertView(isPresented: isPresented))
}

查看和查看型号编码:

struct MyView: View {
    @ObservedObject var viewModel: CustomViewModel = CustomViewModel()
    
    var body: some View {
        VStack {
            switch viewModel.state {
            case .idle:
                Color.clear.onAppear(perform: { viewModel.doSomething() })
            case .showNewView:
                // Navigate
            }
        }.onAppear() {
        viewModel.state = .idle
        }.customAlert(isPresented: .constant(viewModel.showAlert))
    }
}

@MainActor
class CustomViewModel: ObservableObject {
    enum State {
        case idle
        case showNewView
    }
    
    @Published var state = State.idle
    @Published var showAlert = false
    
    func doSomething() {
        if failure {
            self.showAlert = true
        } else {
            self.state = .showNewView
        }
    }
}

我做错了什么,或者为什么我无法解除警报?
感谢您的关注!

lhcgjxsq

lhcgjxsq1#

首先,我们希望@Binding var isPresented: Bool位于CustomAlertView中,并在init中将其赋值为self._isPresented = isPresented

struct CustomAlertView: ViewModifier {
    @Binding var isPresented: Bool // <-- like this

    init(isPresented: Binding<Bool>) {
        self._isPresented = isPresented // <-- and like this
    }
//...

第二,你肯定不想把isPresented设置为.customAlert(isPresented: .constant(viewModel.showAlert))中的一个 * 常量 *,相反,你需要传递绑定变量:

VStack {
// ...
}
.customAlert(isPresented: $viewModel.showAlert) // <-- bind it here

相关问题