[英]NavigationView issue with "Form" Picker inside NavigationLink child
I have a trouble trying to put my Picker inside NavigationLink child struct.我在尝试将我的 Picker 放入 NavigationLink 子结构时遇到了问题。 MyProblem As you see after some transition (Main Page -> Testing Page -> Picker) and choosing any pickers desire option it navigate me back to Testing Page, but Picker itself stay grey.我的问题正如您在经过一些转换(主页 -> 测试页面 -> 选择器)并选择任何选择器所需选项后看到的那样,它会将我导航回测试页面,但选择器本身保持灰色。 I know that it can be solved by adding NavigationView{}
inside NavigationLink child, but it causes another problem, because i already have NavigationView{}
in the top-level ancestor.我知道可以通过在 NavigationLink 子项中添加NavigationView{}
来解决它,但这会导致另一个问题,因为我已经在顶级祖先中有了NavigationView{}
。 AnotherProblem_1 AnotherProblem_2 AnotherProblem_1 AnotherProblem_2
Code:代码:
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView {
List{
NavigationLink(destination: page2()) {
Text("Food input")
}
NavigationLink(destination: page3()) {
Text("Тesting page")
}
}
.navigationBarTitle("Main page")
}
}
}
struct page3: View {
@State private var selectedFlavor = Flavor.chocolate
enum Flavor: String, CaseIterable, Identifiable {
case chocolate
case vanilla
case strawberry
var id: String { self.rawValue }
}
var body: some View {
NavigationView {
Form {
Section {
Picker("Flavor", selection: $selectedFlavor) {
Text("Chocolate").tag(Flavor.chocolate)
Text("Vanilla").tag(Flavor.vanilla)
Text("Strawberry").tag(Flavor.strawberry)
}
Text("Selected flavor: \(selectedFlavor.rawValue)")
}
}
}
}
}
UPDATE: Find out that my console is trying to tell me: "Returning an empty, disconnected UIBarButtonItem to fulfill the non-null contract" I don't know what that exactly mean, but i found strange solution:更新:发现我的控制台试图告诉我:“返回一个空的、断开连接的 UIBarButtonItem 以履行非空合同”我不知道这到底是什么意思,但我发现了一个奇怪的解决方案:
.navigationViewStyle(StackNavigationViewStyle())
Apple documentation doesn't say anything about it, but if you set it up like this:苹果文档对此没有任何说明,但如果你这样设置:
struct ContentView: View {
var body: some View {
NavigationView {
List{
NavigationLink(destination: page2()) {
Text("Food input")
}
NavigationLink(destination: page3()) {
Text("Тesting page")
}
}
.navigationBarTitle("Main page")
}
.navigationViewStyle(StackNavigationViewStyle())
}
}
All problem will gone.所有问题都会消失。 Please explain me how it works, i really stuck in that.请向我解释它是如何工作的,我真的坚持下去。
struct ContentView: View {
var body: some View {
NavigationView {
List{
NavigationLink(destination: page2()) {
Text("Food input")
}
NavigationLink(destination: page3()) {
Text("Тesting page")
}
}
.navigationBarTitle("Main page")
}
.navigationViewStyle(StackNavigationViewStyle())
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.