简体   繁体   English

当视图嵌入模式表中时,NavigationLink 不起作用

[英]NavigationLink doesn't work when View is embedded in a modal sheet

Weird problem.奇怪的问题。 I have a view called SecondView which has a NavigationLink like so:我有一个名为SecondView的视图,它有一个像这样的NavigationLink

struct SecondView: View {
    var body: some View {
        NavigationStack {
            List {
                Section {
                    EmptyView()
                }
                //PowerUp Section
                Section {
                    VStack {
                        NavigationLink (destination: Powerup()) {
                            Text("2")
                        }
                        .isDetailLink(false)
                    }
                }
            }
        }
    }
}

This view is embedded in a modal view.此视图嵌入在模态视图中。 The NavigationLink works fine when I preview SecondView on Xcode but doesn't when I preview the modal sheet.当我在SecondView上预览SecondView时, NavigationLink工作正常,但当我预览模态表时,NavigationLink 工作正常。 Doesn't work on an actual device or simulator too.也不适用于实际设备或模拟器。 Looks like a hierarchy problem or something like this.看起来像是层次结构问题或类似问题。 Can someone tell me where I'm wrong?有人可以告诉我哪里错了吗? Thanks.谢谢。

Since the old NavigationLink(destination:) has some severe limitations, you are supposed to use NavigationLink(value:) and .navigationDestination(for:) when using the new NavigationStack , eg由于旧的NavigationLink(destination:)有一些严重的限制,您应该在使用新的NavigationStack时使用NavigationLink(value:).navigationDestination(for:) ,例如

NavigationStack {
    List(parks) { park in
        NavigationLink(park.name, value: park)
    }
    .navigationDestination(for: Park.self) { park in
        ParkDetails(park: park)
    }
}

Auto-solved the problem.自动解决了问题。 I was embedding SecondView in the modal in the wrong way.我以错误的方式将SecondView嵌入到模态中。 The view should be wrapped in a NavigationView and a NavigationStack first.视图应首先包装在NavigationViewNavigationStack中。 Maybe it's helpful to someone else.也许这对其他人有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NavigationLink 的目标视图在变量更改时不会更新 - NavigationLink's destination view doesn't update when variables change SwiftUI NavigationLink 不适用于 Spacer - SwiftUI NavigationLink doesn't work with Spacer @EnvironmentObject 通过 navigationLink 不能很好地工作 - @EnvironmentObject doesn't work well through navigationLink NavigationLink、isActive 在 SwiftUI 中不起作用 - NavigationLink, isActive doesn´t work in SwiftUI 我正在使用 NavigationLink 在视图之间导航,但它在 SwiftUI 的单个视图中不起作用 - I am using NavigationLink to navigate between views and it doesn't work in a single view in SwiftUI iOS15 - 如果新视图的出现更新 ObservedObject,则第一个 NavigationLink 不起作用 - iOS15 - First NavigationLink doesn't work if onAppear of new view updates ObservedObject SwiftUI ToolbarItem 不显示来自 NavigationLink 的视图 - SwiftUI ToolbarItem doesn't present a View from a NavigationLink SwiftUI - List / ForEach 结合 NavigationLink 和 isActive 不能正常工作 - SwiftUI - List / ForEach in combination with NavigationLink and isActive doesn't work properly NavigationLink 不适用于 SwiftUI 列表中的 ScrollView - NavigationLink doesn't work with ScrollView inside a List in SwiftUI 如何关闭工作表并在新视图中打开 NavigationLink? - How to dismiss a Sheet and open a NavigationLink in a new View?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM