Search
 
SCRIPT & CODE EXAMPLE
 

SWIFT

add callbacks to view swiftui

struct MyComponen: View {
    @Binding var alert: String
    var action: (() -> Void)?
    var body: some View {
        VStack {
            Text("Alert: (alert)")
            if nil != action {
                Button(action: action!) {
                    Text("Action")
                }
            }
        }
    }
}

extension MyComponen {

    func foo(perform action: @escaping () -> Void ) -> Self {
         var copy = self
         copy.action = action
         return copy
     }
}

struct TestCustomModifier: View {
    @State var message = "state 2"
    var body: some View {
        VStack {
            MyComponen(alert: .constant("state 1"))
            MyComponen(alert: $message).foo(perform: {
                print(">> got action")
            })
        }
    }
}

struct TestCustomModifier_Previews: PreviewProvider {
    static var previews: some View {
        TestCustomModifier()
    }
}
Comment

PREVIOUS NEXT
Code Example
Swift :: swiftui foreach enum not all cases 
Swift :: swift add navigation bar 
Swift :: swiftui radio button 
Swift :: accessing tab bar item action swift 
Swift :: deselect all cell in collectionview 
Swift :: How to convert String into Array of character 
Swift :: how to center vertically scrollview swiftui 
Swift :: Single Line Comment 
Swift :: parse int in swift 
Swift :: swift add enum storyboard 
Swift :: swift view 
Swift :: Swift Variable names cannot start with numbers 
Swift :: swift isKindOf 
Swift :: initializer generator xcode swift 
Swift :: set color of indicator line in collectionview swift 
Swift :: Swift Autoclosure 
Swift :: procedural audio with swift 
Swift :: separator style swiftui list 
Swift :: appendBytes: Lengt: SWIFT 
Swift :: Computed Property In Extension Swift 
Swift :: xcode macosx have view resize when window size changed 
Swift :: how request prefix of string in swift 
Swift :: swift 1 to n array 
Swift :: mp3 player with swift iOS & macOS 
Swift :: swift enumeration 
Ruby :: ruby struct 
Ruby :: brew update ruby 
Ruby :: hello world in ruby 
Ruby :: rails excep 
Ruby :: rails find_by order 
ADD CONTENT
Topic
Content
Source link
Name
1+8 =