【问题标题】:Swift UI passing a binding to another view for alert with an initSwift UI 将绑定传递给另一个视图以通过 init 发出警报
【发布时间】:2021-04-15 23:34:17
【问题描述】:

我可以在没有初始化的情况下将绑定传递给另一个视图,并且它工作得很好。但是,如果我尝试使用具有 init 的视图,我将无法克服错误。

struct ContentView: View {
    // MARK: - PROPERTIES
    @State private var showAlert = false
    @State private var alert: Alert? = nil
    @State private var alertTitle = ""
    @State private var alertMessage = ""
    
    // MARK: - BODY
    var body: some View {
        VStack {
            Button(action: {
                alertTitle = "Alert 1"
                alertMessage = "From Main View"
                showAlert.toggle()
            }, label: {
                Text("Show Parent Alert")
            })
            
            // MARK: - TESTVIEW1
            TestView1(showAlert: $showAlert, alert: $alert, alertTitle: $alertTitle, alertMessage: $alertMessage)
        } // END:VSTACK
        .alert(isPresented: $showAlert, content: {
            Alert(title: Text(alertTitle), message: Text(alertMessage), dismissButton: .default(Text("Close")))
        })
    }
}

struct TestView1: View {
    // MARK: - PROPERTIES
    @Binding var showAlert: Bool
    @Binding var alert: Alert?
    @Binding var alertTitle: String
    @Binding var alertMessage: String
    
    // MARK: - BODY
    var body: some View {
        VStack {
            Button(action: {
                alertTitle = "Alert 2"
                alertMessage = "From TestView1"
                showAlert.toggle()
            }, label: {
                Text("Show View-1 Alert")
            })
            
            TestView2(showAlert: $showAlert, alert: $alert, alertTitle: $alertTitle, alertMessage: $alertMessage)
        } // END:VSTACK
    }
}

这很好,但如果我有一个 init,我会收到错误“无法将 'Alert?.Type' 类型的值分配给类型 'Alert'”

struct TestView1: View {
    // MARK: - PROPERTIES
    @Binding var showAlert: Bool
    @Binding var alert: Alert?
    @Binding var alertTitle: String
    @Binding var alertMessage: String
    
    init(showAlert: Binding<Bool>, alert: Binding<Alert>, alertTitle: Binding<String>, alertMessage: Binding<String>) {
        self.showAlert = false
        self.alert = Alert?
        self.alertTitle = ""
        self.alertMessage = ""
    }

【问题讨论】:

    标签: swiftui binding


    【解决方案1】:

    如果您有一个接受 Binding 的 init,则需要将 Binding(而不是值)分配给 @Binding 属性。您可以使用 _ 前缀来做到这一点。

    init(showAlert: Binding<Bool>, 
         alert: Binding<Alert>, 
         alertTitle: Binding<String>, 
         alertMessage: Binding<String>) {
    
       self._showAlert = showAlert
       self._alert = alert
       self._alertTitle = alertTitle
       self._alertMessage = alertMessage
    }
    

    【讨论】:

    • 感谢您的回复。这样做的全部原因是这是我的单元格视图,并且我在父级中已经有一个 .alert 和 .sheet ,因此试图找到一种在子单元格视图中显示警报的方法。我按照您的指定进行切换,但出现错误无法将类型 'Binding' 的值分配给类型 'Binding
    • 再次感谢。我必须设置为可选。在此之后我实际上正在通过另一个级别,但工作。希望操作表是相同的 init(commentVM: CommentViewModel, showAlert: Binding, alert: Binding, alertTitle: Binding, alertMessage: Binding)
    猜你喜欢
    • 2015-10-13
    • 1970-01-01
    • 2022-06-10
    • 2021-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-14
    相关资源
    最近更新 更多