SwiftUI 教程 PresentationButton 错误 [英] SwiftUI Tutorial PresentationButton Bug

查看:21
本文介绍了SwiftUI 教程 PresentationButton 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始尝试在 WWDC 2019 上发布的新 SwiftUI 框架,并在 https 上开始教程://developer.apple.com/tutorials/swiftui.

I started to experiment with the new SwiftUI framework, announced on the WWDC 2019 and started the tutorial on https://developer.apple.com/tutorials/swiftui.

现在我要通过 PresentationButton 将配置文件连接到主屏幕.更准确地说,我指的是 Home.swift 中的这部分代码:

Now I came to the point where to connect the Profile to the HomeScreen via the PresentationButton. More precisely I am talking about this section of code in Home.swift:

            .navigationBarItems(trailing:
                PresentationButton(
                    Image(systemName: "person.crop.circle")
                        .imageScale(.large)
                        .accessibility(label: Text("User Profile"))
                        .padding(),
                    destination: ProfileHost()
                )
            )

当我第一次单击该按钮时,配置文件表看起来很好,但是当我关闭它然后再次单击该按钮时,没有任何反应.

When I first click on the button the Profile Sheet appears just fine, but when I dismiss it and then click on the button again nothing happens.

有谁知道为什么会这样?

Does anyone know why this is the case ?

提前致谢

推荐答案

看起来像是 SwiftUI 中的一个错误.这可能与 onDisappear 从未被调用的事实有关.您可以通过添加

It looks like a bug in SwiftUI. It is probably linked to the fact that onDisappear is never called. You can verify that by adding

.onAppear{
  print("Profile appeared")
}.onDisappear{
  print("Profile disappeared")
}

ProfileHost 视图.一个 appear 应该被一个 disappear 平衡才能完成解雇是有道理的.

to ProfileHost view. It would make sense that an appear should be balanced by a disappear for the dismissal to be complete.

可以通过实现一个返回依赖"状态变量的 PresentationButton 的函数来解决这个问题.

It is possible to work around it by implementing a function that returns a PresentationButton that "depends" on a state variable.

@State var profilePresented: Int = 0
func profileButton(_ profilePresented: Int) -> some View {
  return PresentationButton(
    Image(systemName: "person.crop.circle")
      .imageScale(.large)
      .accessibility(label: Text("User Profile"))
      .padding(),
    destination: ProfileHost(),
    onTrigger: {
      let deadlineTime = DispatchTime.now() + .seconds(2)
      DispatchQueue.main.asyncAfter(deadline: deadlineTime, execute: {
        self.profilePresented += 1
      })
  })
}

并替换

.navigationBarItems(trailing:
      PresentationButton(
          Image(systemName: "person.crop.circle")
              .imageScale(.large)
              .accessibility(label: Text("User Profile"))
              .padding(),
          destination: ProfileHost()
      )
  )

.navigationBarItems(trailing: self.profileButton(self.profilePresented))

我强烈建议不要使用这种解决方案",只需将错误报告给 Apple.

I highly recommend to not use this "solution" and just report the bug to Apple.

这篇关于SwiftUI 教程 PresentationButton 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆