SwiftUI NavigationView 试图弹出到丢失的目的地(Monoceros?) [英] SwiftUI NavigationView trying to pop to missing destination (Monoceros?)

查看:43
本文介绍了SwiftUI NavigationView 试图弹出到丢失的目的地(Monoceros?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Xcode 12 进行 iOS 14.0 部署.

I'm using Xcode 12 with deployment for iOS 14.0.

  • 我的主屏幕有一个 NavigationView
  • 在 NavigationView 中有一个 TabView(有 4 个标签)
  • 每个选项卡内都有包含按钮和导航链接的子视图

应用程序上的导航功能正常(当我单击其中一个子视图上的 NavigationLink 时,它导航到正确的视图,当我单击后退按钮时,它会关闭视图.)但是,当我单击后退时按钮,控制台打印以下错误:

The navigation on the app is functioning correctly (when I click a NavigationLink on one of the subviews, it navigates to the correct view and when I click the back button, it dismisses the view.) However, when I click the back button, the console prints the following error:

Trying to pop to a missing destination at /Library/Caches/com.apple.xbs/Sources/Monoceros/Monoceros-103/Shared/NavigationBridge_PhoneTV.swift:337

除了错误日志之外,该应用程序运行良好,所以我打算暂时忽略该错误...但我想知道这意味着什么?我的代码中没有任何名为Monoceros"的内容.我猜这与 TabView 作为 NavigationView 的子视图有关?

Aside from the error log, the app is functioning fine, so I'm planning to just ignore the error for now... but I'm wondering what it means? I don't have anything within my code named "Monoceros". I'm guessing it has something to do with the TabView being a subview of the NavigationView?

几个月后,这个问题仍然存在.这是可重现的代码.打开 ContentView(),在 FirstScreen() 上单击 NavigationLink,然后单击后退按钮.它会打印出 Monoceros 哈哈

Several months later, this issue still persists. Here is reproducible code. Open the ContentView(), on the FirstScreen() click on the NavigationLink, then click the back button. It will print out Monoceros lol

import SwiftUI

struct ContentView: View {
    var body: some View {
        NavigationView {
            TabView {
                FirstScreen()
                    .tabItem {
                        Text("One")
                        Image(systemName: "house.fill")
                    }
                
                Text("Second Screen")
                    .tabItem {
                        Text("Two")
                        Image(systemName: "heart.fill")
                    }
            }
        }
    }
}

struct FirstScreen: View {
    var body: some View {
        NavigationLink("Click here", destination: Text("Final Screen"))
        // Click the back button on FinalScreen prints:
        //Trying to pop to a missing destination at /Library/Caches/com.apple.xbs/Sources/Monoceros_Sim/Monoceros-120/Shared/NavigationBridge_PhoneTV.swift:341
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

推荐答案

不幸的是,这是一个活动问题,TabView 放置在 NavigationView 中.

Unfortunately this is an active issue with a TabView placed inside NavigationView.

如果您将 NavigationView 放在 TabView 内,则错误不会自行显示,但这当然会导致从 Final 中显示选项卡屏幕,您可能正在尝试避免.

The error would not manifest itself if you placed your NavigationView inside the TabView, but this of course would lead to the tabs being displayed from within your Final Screen, which you are probably trying to avoid.

目前没有解决方法,到目前为止,我们还需要等待 Apple 正确实现 TabViews 的相应 .navigationBarHidden().

There is currently no work-around for this, and as of to date we need to wait for Apple to properly implement the corresponding .navigationBarHidden() of TabViews as well.

问题和意外行为已经报告在将 TabView 嵌入到 NavigationView 中时,但是如果您已经彻底测试了您的应用并且没有发现特别的问题,那么可以肯定地说您可以坚持这个方法.

Problems and unexpected behaviours have been reported when embedding a TabView into a NavigationView, however if you have tested your app thoroughly and found no particular problems, it is safe to say that you can stick to this method.

或者,您必须手动构建一个 TabView 组件,如下所示:

Alternatively you will have to build a TabView component manually, like below:

import SwiftUI


enum Tab {
    case house, heart
}

struct TabView: View {
    @Binding var tabIdx: Tab
    
    var body: some View {
        HStack {
            Group {
                Spacer()
                
                Button (action: {
                    self.tabIdx = .house
                }) {
                    VStack{
                        Image(systemName: "house.fill")
                        Text("House")
                            .font(.system(size: 10))

                    }
                }
                .foregroundColor(self.tabIdx == .house ? .blue : .secondary)
                
                Spacer()
                
                Button (action: {
                    self.tabIdx = .heart
                }) {
                    VStack{
                        Image(systemName: "heart.fill")
                        Text("Heart")
                            .font(.system(size: 10))

                    }
                }
                .foregroundColor(self.tabIdx == .heart ? .blue : .secondary)
                
                Spacer()
            }
        }
        .padding(.bottom, 30)
        .padding(.top, 10)
        .background(Color(red: 0.1, green: 0.1, blue: 0.1))
        .font(.system(size: 30))
        .frame(height: 80)
    }
}

struct FirstScreen: View {
    var body: some View {
        NavigationLink("Click here", destination: Text("Final Screen"))
            .font(.system(size:20))
    }
}

struct ContentView: View {
    @State var tabIdx: Tab = .house
    
    var body: some View {
        NavigationView {
            VStack(spacing: 20) {
                Spacer()
                if tabIdx == .house {
                    FirstScreen()
                } else if tabIdx == .heart {
                    Text("Second Screen")
                }
                Spacer(minLength: 0)
                TabView(tabIdx: self.$tabIdx)
            }
            .ignoresSafeArea()

        }
    }

}

上述错误在this中有详细说明博客文章,您可以参考以获取更多参考和更多示例.

The above bug is well detailed in this blog post, which you could consult for further reference and more examples.

这篇关于SwiftUI NavigationView 试图弹出到丢失的目的地(Monoceros?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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