Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing object and allowing dismiss? #7

Open
ozumado opened this issue Dec 1, 2019 · 3 comments
Open

Passing object and allowing dismiss? #7

ozumado opened this issue Dec 1, 2019 · 3 comments

Comments

@ozumado
Copy link

ozumado commented Dec 1, 2019

Hello there,

I'm just learning SwiftUI and I faced a problem while using this library (which is awesome and simple btw.). Is it possible to pass an object to modal view and enable dismiss action?

ModalLink(destination: DetailedView(item:item).init(dismiss:))

Doesn't seem to work... I'm either able to only pass an object or enable dismiss action.

ModalLink(destination: DetailedView(item:item)
ModalLink(destination: DetailedView.init(dismiss:))

Any clues?

@diniska
Copy link
Owner

diniska commented Dec 23, 2019

Hello @ozumado. Sorry for the delay with answer, just saw your message.
There is definitely no restriction on passing the object and dismiss closure at the same time.
In fact, ModalLink accepts a closure that is supposed to return a View. So, instead of passing a short version of init closure for the DetailView, we can use a full version like this:

ModalLink(destination: { dismiss in DetailedView(item: "Item", dismiss: dismiss)}) {
    Text("Present")
}

Or you can rewrite the same in a next way depending on the syntax preferences:

ModalLink(
    destination: { DetailedView(item: "Item", dismiss: $0) },
    label: { Text("Present") }
)

Please, let me know if you have some other questions or improvements suggestions about the library.

Repository owner deleted a comment from pmark Dec 23, 2019
@ozumado
Copy link
Author

ozumado commented Dec 25, 2019

Thanks for your reply, I decided to use native way to close a View instead (swift @Environment(\.presentationMode) var presentationMode and swift self.presentationMode.wrappedValue.dismiss()) and it works fine now except for one thing: when presenting a ModalView with child ModalLinks once you scroll that ModalView it "clicks" the links and open that child ModalView. This also occurs when using native .sheet(), so it must be some Apple Bug.
Currently I have no idea how to prevent it from opening links upon scroll...

@ozumado
Copy link
Author

ozumado commented Dec 25, 2019

Ok, so I overcome this issue by using .onTapGesture() instead of Button() while using native .sheet(). It looks like Button is the issue here.

I'm not an expert, but maybe this change could fix the issue in the library:
Change this:

    public var body: some View {
        Button(action: presentModalView){ label }
    }

To this:

    public var body: some View {
        label.onTapGesture() { self.presentModalView() }
    }

But now the "fake button" doesn't have the same feel as real one - not changing opacity while pressed down, so I currently overcome this issue by creating a Button that does nothing, and applying .onTapGesture() that trigger the action:

    public var body: some View {
        Button(action: { }) { label.onTapGesture() { self.presentModalView() } }
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants