4 Replies
      Latest reply on Nov 17, 2019 12:04 PM by Jim Dovey
      ruckstar Level 1 Level 1 (0 points)

        In section 2 of "Working with UI Controls" the instructions state to add `@Environment(\.editMode) var mode` to the `ProfileHost` view and use that binding to show either the profile summary or the profile editor:

         

        if self.mode?.value == .inactive { 
            ProfileSummary(profile: profile) 
        } else { 
            Text("Profile Editor") 
        }

         

        When previewing the canvas in live mode it seems the `mode` variable is always nil and thus it never shows the profile editor. Tapping the edit button seems to have no effect.

        • Re: SwiftUI Tutorial EditMode
          tomwilson Level 1 Level 1 (10 points)

          I believe it's a bug - if you actually run your app it will work as expected

          • Re: SwiftUI Tutorial EditMode
            CypherPoet Level 1 Level 1 (0 points)

            FWIW, I'm having the same issue (but things _do_ behave correctly when I run the app).

             

            Xcode 11 GM

            MacOS Catalina Beta 8

            • Re: SwiftUI Tutorial EditMode
              hakim.id.id Level 1 Level 1 (0 points)

              I'm having the same problem. But yeah it works when you run the app on the simulator. So if any Apple guys seeing this, there's something other weird thing too. The animation of the HikeView() when the "chevron.right.circle" button is pressed is totally off.

              • Re: SwiftUI Tutorial EditMode
                Jim Dovey Level 3 Level 3 (270 points)

                This happens because there's nothing adding a binding to an EditMode variable in the environment, so the value you pull from the environment is nil. A real UIHostingController does this, but it appears the canvas scaffolding doesn't, so you have to add it yourself. You can't just use plain @State to generate an @Binding inside ProfileHost_Previews.previews, because it'll crash complaining that you can't do that outside of a body implementation. So, when I was going through this, I built a wrapper to use for the preview:

                 

                struct EditModeInPreview<Content: View> : View {
                    var content: Content
                    @State var editMode: EditMode = .inactive
                    
                    var body: some View {
                        content.environment(\.editMode, $editMode)
                    }
                    
                    init(@ViewBuilder content: () -> Content) {
                        self.content = content()
                    }
                }
                
                struct ProfileHost_Previews: PreviewProvider {
                    static var previews: some View {
                        EditModeInPreview {
                            ProfileHost()
                        }
                        .environmentObject(UserData())
                    }
                }
                

                 

                Share and Enjoy!