代码之家  ›  专栏  ›  技术社区  ›  dunoiww

从全屏返回时如何触发onAppear

  •  0
  • dunoiww  · 技术社区  · 2 年前

    onAppear不触发

    struct ProfileView: View {
        @StateObject var viewModel = ProfileViewViewModel()
        var body: some View {
            NavigationView {
                VStack {
                    if let user = viewModel.user {
                        profile(user: user)
                    } else {
                        Text("Loading Profile...")
                    }
                }
                .navigationTitle("Profile")
            }
            .onAppear {
                viewModel.fetchUser() //this is the problem
            }
            .fullScreenCover(isPresented: $viewModel.showingPreview) {
                PreviewAvatarView()
            }
        }
    }
    

    我意识到onAppear并没有触发当我解除全屏覆盖。

    2 回复  |  直到 2 年前
        1
  •  1
  •   lorem ipsum    2 年前

    而不是使用 onAppear 你可以使用 task

    struct ProfileView: View {
        @StateObject var viewModel = ProfileViewViewModel()
        var body: some View {
            NavigationView {
                VStack {
                    if let user = viewModel.user {
                        profile(user: user)
                    } else {
                        Text("Loading Profile...")
                    }
                }
                .navigationTitle("Profile")
            }
            .task(id: viewModel.showingPreview) {
                guard !viewModel.showingPreview else {return} //Check for false
        
                viewModel.fetchUser() //this is the problem
            }
            .fullScreenCover(isPresented: $viewModel.showingPreview) {
                PreviewAvatarView()
            }
        }
    }
    
    

    任务 将运行 on出现 Bool false

        2
  •  0
  •   ScottM    2 年前

    这个 onAppear 不会因为你的基本观点没有出现而触发——覆盖它的观点正在消失,这不是一回事。

    然而 fullScreenCover 有一个很少使用的可选参数, onDismiss ,可以满足您的需求,例如:

    .fullScreenCover(
      isPresented: $viewModel.showingPreview,
      onDismiss: { viewModel.fetchUser() }
    ) {
      PreviewAvatarView()
    }
    

    Apple documentation

        3
  •  0
  •   Kapil Shanbhag    2 年前

    你可以试试这个解决方案。

    struct ProfileView: View {
        @StateObject var viewModel = ProfileViewViewModel()
        var body: some View {
            NavigationView {
                VStack {
                    if let user = viewModel.user {
                        profile(user: user)
                    } else {
                        Text("Loading Profile...")
                    }
                }
                .navigationTitle("Profile")
            }
            .onAppear {
                viewModel.fetchUser() //this is the problem
            }
            .fullScreenCover(isPresented: $viewModel.showingPreview) {
                PreviewAvatarView()
            }
            .onChange(of: viewModel.showingPreview) { isFullScreenOpen in
                if !isFullScreenOpen {
                    viewModel.fetchUser()
                }//Executed only when full screen cover is closed.
            }
        }
    }
    

    每当工作表关闭时,就会执行viewModel.fetchUser()。