简体   繁体   中英

reuse code/properties for several views in swiftui

we have several SwiftUI screens that are presented as sheet. all of then can be dismissed by clicking a button.

so basically all of them have these 2 in common:

@Environment(\.presentationMode) var presentationMode
func dismiss() {
    presentationMode.wrappedValue.dismiss()
}

how can i declare them only once and just reuse them only in specific views? i cannot use inheritance since they are stucts, extensions cannot contain state (except using a holder struct) and would add these to all instances of the same view type.

The .presentationMode is available throughout current view hierarchy, so we can use this feature to wrap & manage dismiss in some modifier.

Here is a demo of solution based on button style, so any button can be specified as dismissing and it will dismiss current presentation.

Prepared with Xcode 12.1 / iOS 14.1

在此处输入图像描述

struct TestReuseDismissed: View {
    @State private var isActive = false
    var body: some View {
        Button("Show Sheet") {
            isActive = true
        }
        .sheet(isPresented: $isActive) {
            Button("Dismiss", action: { 
                   // do something before dismiss here !!
                })
                .buttonStyle(DismissButtonStyle())
        }
    }
}

struct DismissButtonStyle: PrimitiveButtonStyle {
    @Environment(\.presentationMode) var presentationMode

    func makeBody(configuration: Configuration) -> some View {
        Button(action: {
            configuration.trigger()
            dismiss()
        }) { configuration.label }
    }

    func dismiss() {
        presentationMode.wrappedValue.dismiss()
    }
}

backup

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM