Optional Binding in parameter SwiftUI

前端 未结 1 1676
无人及你
无人及你 2020-12-12 02:51

Here are my optional binding

@Binding var showSheetModifFile : Bool?
@Binding var fileToModify : File?

init( showSheetModifFile : Binding? = nil         


        
相关标签:
1条回答
  • 2020-12-12 03:09

    There is special Binding constructor for this purpose

    SingleFileView(showSheetModifFile: Binding(self.$showModifFileSheet), 
       fileToModify: Binding(self.$fileToModify))
    

    Update: alternate solution

    struct FileDemoView: View {
        @State var showModifFileSheet : Bool? = false
        @State var fileToModify : File? = File()
    
        var body: some View {
            SingleFileView(showSheetModifFile: $showModifFileSheet, fileToModify: $fileToModify)
        }
    
    }
    
    
    struct SingleFileView: View {
        @Binding var showSheetModifFile : Bool?
        @Binding var fileToModify : File?
    
    
        init(showSheetModifFile : Binding<Bool?> = .constant(nil), fileToModify : Binding<File?> = .constant(nil)) {
            _showSheetModifFile = showSheetModifFile
            _fileToModify = fileToModify
        }
    
        var body: some View {
            Text("")
        }
    }
    
    0 讨论(0)
提交回复
热议问题