What I wanted to do
Posted: Sat Jan 18, 2025 5:59 am
I want to change the content displayed in the half-modal for each item selected on the sheet's display screen.
[Image]
Conclusion first
There are two types of sheets, but in this case the peru email address problem was solved by changing it to use sheet(item:onDismiss:content:) .
An explanation of what I was stuck on and sample source code are provided below.
Where I got hooked
First of all, I thought that I could use a sheet to display a half-modal, so I implemented it as follows.
Define the options with an enum
sheet(isPresented:onDismiss:content:)Use for modal display
When an option is tapped, isPresentSheetset it to true and display the modal.
Pass the selected item to the View in the modal as an argumentThe official explanation is...?
If you watch the WWDC session, you will see that the concepts of Identity, Lifetime, and Dependencies are important when creating apps with SwiftUI.
*Reference: WWDC2021 "Demystify SwiftUI" (Session Video)
Identity: This allows SwiftUI to recognize elements as the same or different across multiple updates to your app.
Lifetime: For SwiftUI to track the existence of views and data over time.
Dependencies: Helps SwiftUI understand when it needs to update your interface and why
It seems that these three things combine to allow SwiftUI to update the view.
I think that the "comply with the Identifiable protocol" that I wrote in my previous prediction applies to Identity, and " monitor @Statethe state " applies to Lifetime. I think that's where we connect a bit.onChange
It is difficult to pinpoint the exact cause, but if you use these three concepts to sort out whether there is anything missing in the source code you wrote and whether the dependencies are formed correctly, you may be able to find a clue to solving the bug even if it occurs!
[Image]
Conclusion first
There are two types of sheets, but in this case the peru email address problem was solved by changing it to use sheet(item:onDismiss:content:) .
An explanation of what I was stuck on and sample source code are provided below.
Where I got hooked
First of all, I thought that I could use a sheet to display a half-modal, so I implemented it as follows.
Define the options with an enum
sheet(isPresented:onDismiss:content:)Use for modal display
When an option is tapped, isPresentSheetset it to true and display the modal.
Pass the selected item to the View in the modal as an argumentThe official explanation is...?
If you watch the WWDC session, you will see that the concepts of Identity, Lifetime, and Dependencies are important when creating apps with SwiftUI.
*Reference: WWDC2021 "Demystify SwiftUI" (Session Video)
Identity: This allows SwiftUI to recognize elements as the same or different across multiple updates to your app.
Lifetime: For SwiftUI to track the existence of views and data over time.
Dependencies: Helps SwiftUI understand when it needs to update your interface and why
It seems that these three things combine to allow SwiftUI to update the view.
I think that the "comply with the Identifiable protocol" that I wrote in my previous prediction applies to Identity, and " monitor @Statethe state " applies to Lifetime. I think that's where we connect a bit.onChange
It is difficult to pinpoint the exact cause, but if you use these three concepts to sort out whether there is anything missing in the source code you wrote and whether the dependencies are formed correctly, you may be able to find a clue to solving the bug even if it occurs!