【问题标题】:SwiftUI Tutorial PresentationButton BugSwiftUI 教程 PresentationButton 错误
【发布时间】:2019-06-10 09:50:12
【问题描述】:

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

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

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

当我第一次单击该按钮时,配置文件表显示得很好,但是当我将其关闭然后再次单击该按钮时,没有任何反应。

有人知道为什么会这样吗?

提前致谢

【问题讨论】:

    标签: swift xcode swiftui


    【解决方案1】:

    它看起来像 SwiftUI 中的一个错误。 这可能与从未调用过onDisappear 的事实有关。 您可以通过添加来验证这一点

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

    ProfileHost 查看。 appear 应该与 disappear 平衡以完成解雇是有道理的。

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

    @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。

    【讨论】:

    • 你说得对,onDisappear 方法永远不会被调用。我认为这将是错误的根本原因。已经报告了这个bug,谢谢你的建议
    • 是的。我可以确认这是 SwiftUI 的一个错误,我们应该联系 Apple 获取这些信息。希望它可以在下一个测试版中修复
    【解决方案2】:

    解决这个问题最简单的方法是保留destination: 参数并在花括号中包含Image 对象:

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

    【讨论】:

    • 这是我正在使用的,按钮仍然只触发一次。我认为这是等待 beta3 的情况。 PresentationButton(destination: MessageListView().environmentObject(userData)) { Image(systemName: "list.bullet") }
    【解决方案3】:

    这已在 Beta 3 中得到修复。我也遇到了同样的问题,PresentationButton(现为 PresentationLink)在嵌入 .navigationBarItems 时仅触发一次。

    【讨论】:

    • 我正在使用 Beta 3,但这仍然不适合我。愿意分享您的工作解决方案吗?
    【解决方案4】:

    这是在 Xcode 11 Beta2 中解决的错误:https://developer.apple.com/documentation/xcode_release_notes/xcode_11_beta_2_release_notes

    使用更新后的 API,以下内容应该可以工作:

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

    【讨论】:

    • 使用 Xcode 11 Beta2 我仍然得到相同的行为:即按钮将第一次显示配置文件,但不会再次触发。我查看了发行说明,找不到与PresentationButton 相关的任何内容(实际上并没有提及)。对我来说似乎是在等待 beta 3...
    • 这部分代码给了我不同类型的错误,它显示“在调用中缺少参数 #1 的参数,插入 ', '”。如果我给它一个标签,它可以工作,但它显示标签文本而不是人员小部件。谁有解决办法?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多