<tfoot id='tIxRJ'></tfoot>

        <small id='tIxRJ'></small><noframes id='tIxRJ'>

      1. <legend id='tIxRJ'><style id='tIxRJ'><dir id='tIxRJ'><q id='tIxRJ'></q></dir></style></legend>
        • <bdo id='tIxRJ'></bdo><ul id='tIxRJ'></ul>
        <i id='tIxRJ'><tr id='tIxRJ'><dt id='tIxRJ'><q id='tIxRJ'><span id='tIxRJ'><b id='tIxRJ'><form id='tIxRJ'><ins id='tIxRJ'></ins><ul id='tIxRJ'></ul><sub id='tIxRJ'></sub></form><legend id='tIxRJ'></legend><bdo id='tIxRJ'><pre id='tIxRJ'><center id='tIxRJ'></center></pre></bdo></b><th id='tIxRJ'></th></span></q></dt></tr></i><div id='tIxRJ'><tfoot id='tIxRJ'></tfoot><dl id='tIxRJ'><fieldset id='tIxRJ'></fieldset></dl></div>

        为什么我的 SwiftUI 应用程序在 `NavigationView` 中的 `navigationBarItems`

        Why does my SwiftUI app crash when navigating backwards after placing a `NavigationLink` inside of a `navigationBarItems` in a `NavigationView`?(为什么我的 SwiftUI 应用程序在 `NavigationView` 中的 `navigationBarItems` 内放置 `NavigationLink
        <tfoot id='6amzm'></tfoot>

        <small id='6amzm'></small><noframes id='6amzm'>

                  <bdo id='6amzm'></bdo><ul id='6amzm'></ul>

                    <tbody id='6amzm'></tbody>
                  <legend id='6amzm'><style id='6amzm'><dir id='6amzm'><q id='6amzm'></q></dir></style></legend>

                  <i id='6amzm'><tr id='6amzm'><dt id='6amzm'><q id='6amzm'><span id='6amzm'><b id='6amzm'><form id='6amzm'><ins id='6amzm'></ins><ul id='6amzm'></ul><sub id='6amzm'></sub></form><legend id='6amzm'></legend><bdo id='6amzm'><pre id='6amzm'><center id='6amzm'></center></pre></bdo></b><th id='6amzm'></th></span></q></dt></tr></i><div id='6amzm'><tfoot id='6amzm'></tfoot><dl id='6amzm'><fieldset id='6amzm'></fieldset></dl></div>
                • 本文介绍了为什么我的 SwiftUI 应用程序在 `NavigationView` 中的 `navigationBarItems` 内放置 `NavigationLink` 后向后导航时崩溃?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  这已在 iOS 13.3 中修复!

                  最小的可重现示例(Xcode 11.2 beta,适用于 Xcode 11.1):

                  Minimal reproducible example (Xcode 11.2 beta, this works in Xcode 11.1):

                  struct Parent: View {
                      var body: some View {
                          NavigationView {
                              Text("Hello World")
                                  .navigationBarItems(
                                      trailing: NavigationLink(destination: Child(), label: { Text("Next") })
                                  )
                          }
                      }
                  }
                  
                  struct Child: View {
                      @Environment(.presentationMode) var presentation
                      var body: some View {
                          Text("Hello, World!")
                              .navigationBarItems(
                                  leading: Button(
                                      action: {
                                          self.presentation.wrappedValue.dismiss()
                                      },
                                      label: { Text("Back") }
                                  )
                              )
                      }
                  }
                  
                  struct ContentView: View {
                      var body: some View {
                          Parent()
                      }
                  }
                  

                  问题似乎在于将我的 NavigationLink 放在 navigationBarItems 修饰符中,该修饰符嵌套在根视图为 NavigationView.崩溃报告表明,当我向前导航到 Child 然后返回到 Parent 时,我正在尝试弹出一个不存在的视图控制器.

                  The issue seems to lie in placing my NavigationLink inside of a navigationBarItems modifier that's nested inside of a SwiftUI view whose root view is a NavigationView. The crash report indicates that I'm trying to pop to a view controller that doesn't exist when I navigate forward to Child and then back to Parent.

                  Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Tried to pop to a view controller that doesn't exist.'
                  *** First throw call stack:
                  

                  如果我改为将 NavigationLink 放在视图主体中,如下所示,它工作得很好.

                  If I were to instead place that NavigationLink in the body of the view like the below, it works just fine.

                  struct Parent: View {
                      var body: some View {
                          NavigationView {
                              NavigationLink(destination: Child(), label: { Text("Next") })
                          }
                      }
                  }
                  

                  这是 SwiftUI 错误还是预期行为?

                  Is this a SwiftUI bug or expected behavior?

                  我已经在他们的反馈助手中打开了 Apple 的问题,ID 为 FB7423964,以防 Apple 的任何人关心:).

                  I've opened an issue with Apple in their feedback assistant with the ID FB7423964 in case anyone out there from Apple cares to weigh in :).

                  我在反馈助手中打开的工单表明有 10 多个类似的报告问题.他们已使用 Resolution:Potential fix known - For a future OS update 更新了该解决方案.手指交叉,修复很快就会着陆.

                  My open ticket in the feedback assistant indicates there are 10+ similar reported issues. They've updated the resolution with Resolution: Potential fix identified - For a future OS update. Fingers crossed that the fix lands soon.

                  推荐答案

                  这对我来说是个痛点!我离开了它,直到我的大部分应用程序完成并且我有足够的空间来处理崩溃.

                  This was quite a pain point for me! I left it until most of my app was completed and I had the mind space to deal with the crashing.

                  我想我们都同意 SwifUI 有一些非常棒的东西,但调试可能很困难.

                  I think we can all agree that there's some pretty awesome stuff with SwifUI but that the debugging can be difficult.

                  在我看来,我会说这是一个BUG.这是我的理由:

                  In my opinion, I would say that this is a BUG. Here is my rationale:

                  • 如果你将presentationModedismiss调用包裹在大约半秒的异步延迟中,你应该会发现程序将不再崩溃.

                  • If you wrap the presentationMode dismiss call in an asynchronous delay of about a half-second, you should find that the program will no longer crash.

                  DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
                      self.presentationMode.wrappedValue.dismiss()
                  } 
                  

                • 这向我表明,该错误是 SwiftUI 如何与所有其他 UIKit 代码交互以管理各种视图的一种意外行为.根据您的实际代码,您可能会发现如果视图中有一些较小的复杂性,实际上不会发生崩溃.例如,如果您从一个视图中解散到一个具有列表的视图,并且该列表为空,您将在没有异步延迟的情况下发生崩溃.另一方面,如果您在该列表视图中甚至只有一个条目,强制循环迭代以生成父视图,您将看到不会发生崩溃.

                • This suggests to me that the bug is an unexpected behaviour way down deep in how SwiftUI interfaces with all the other UIKit code to manage the various views. Depending on your actual code, you might find that if there is some minor complexity in the view, the crash actually will not happen. For example, if you are dismissing from a view to one that has a list, and that list is empty, you will get a crash without the asynchronous delay. On the other hand, if you have even just one entry in that list view, forcing a loop iteration to generate the parent view, you'll see that the crash will not occur.

                  我不太确定我在延迟中包装解雇呼叫的解决方案有多强大.我必须对其进行更多测试.如果您对此有任何想法,请告诉我!很高兴向您学习!

                  I'm not so sure how robust my solution of wrapping the dismiss call in a delay is. I have to test it much more. If you have ideas on this, please let me know! I'd be very happy to learn from you!

                  这篇关于为什么我的 SwiftUI 应用程序在 `NavigationView` 中的 `navigationBarItems` 内放置 `NavigationLink` 后向后导航时崩溃?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!
                • 相关文档推荐

                  UINavigationController inside a UITabBarController inside a UISplitViewController presented modally on iPhone(UISplitViewController 内的 UITabBarController 内的 UINavigationController 以模态方式呈现在 iPhone 上) - IT屋-程序员软件开发技术分
                  ViewController in UINavigationController orientation change(UINavigationController 中的 ViewController 方向更改)
                  Custom back button in UINavigationController(UINavigationController 中的自定义后退按钮)
                  How to add a navigation controller programmatically in code but not as initial view controller(如何在代码中以编程方式添加导航控制器,但不作为初始视图控制器)
                  The correct way to set a light status bar text color in iOS 7 based on different ViewControllers(iOS 7中基于不同ViewControllers设置灯光状态栏文字颜色的正确方法)
                  View being blocked by UITransitionView after being presented(呈现后被 UITransitionView 阻止的视图)
                  <tfoot id='l3WmW'></tfoot>
                      <legend id='l3WmW'><style id='l3WmW'><dir id='l3WmW'><q id='l3WmW'></q></dir></style></legend>

                        <bdo id='l3WmW'></bdo><ul id='l3WmW'></ul>
                          <i id='l3WmW'><tr id='l3WmW'><dt id='l3WmW'><q id='l3WmW'><span id='l3WmW'><b id='l3WmW'><form id='l3WmW'><ins id='l3WmW'></ins><ul id='l3WmW'></ul><sub id='l3WmW'></sub></form><legend id='l3WmW'></legend><bdo id='l3WmW'><pre id='l3WmW'><center id='l3WmW'></center></pre></bdo></b><th id='l3WmW'></th></span></q></dt></tr></i><div id='l3WmW'><tfoot id='l3WmW'></tfoot><dl id='l3WmW'><fieldset id='l3WmW'></fieldset></dl></div>
                            <tbody id='l3WmW'></tbody>

                          <small id='l3WmW'></small><noframes id='l3WmW'>