diniska / modal-view

Present Modal view with Swift UI in the same way you would do with NavigationView
MIT License
92 stars 9 forks source link

Getting previews to work when view is in another file #9

Closed niklaswallerstedt closed 4 years ago

niklaswallerstedt commented 4 years ago

Hey,

Just tried out the modal and seems really nice to work with. Kind of new to SwiftUI (and iOS development in general) and having some issues getting the preview to work when I extract the MyModalView struct to a separate file.

Getting Missing argument for parameter 'dismiss' in call and unsure how to resolve this in the preview.

//  MyModalView.swift

import SwiftUI

struct MyModalView: View {
    var dismiss: () -> ()
    var body: some View {
        Button(action: dismiss) {
            Text("Dismiss")
        }
    }
}

struct MyModalView_Previews: PreviewProvider {
    static var previews: some View {
        MyModalView()  // Missing argument for parameter 'dismiss' in call
    }
}
//  ContentView.swift

import SwiftUI
import ModalView

struct ContentView: View {
    var body: some View {
        ModalPresenter {
            ModalLink(destination: MyModalView.init(dismiss:)) {
                Text("Main view")
            }
        }
    }
}

struct MyModalView: View {
    var dismiss: () -> ()
    var body: some View {
        Button(action: dismiss) {
            Text("Dismiss")
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
niklaswallerstedt commented 4 years ago

This seems to work, need to dive further down into SwiftUI.

struct MyModalView_Previews: PreviewProvider {
    static var previews: some View {
        MyModalView {
            return
        }
    }
}