SwiftUI - NavigationLink 返回上一个视图
SwiftUI - NavigationLink to go back to previous view
我正在通过构建一个具有 "Settings-View" 的应用程序来测试 SwiftUI,我们称它为 ViewB。此视图有一个列表,您可以在其中 select 一种语言。
第一个视图,ViewA 有 2 个按钮 "Open" 或 "Select language".
我想做的是,从ViewA开始,打开ViewB,select一种语言,自动回到ViewA。这就是我所做的:
视图A
var body: some View {
NavigationView {
VStack(alignment: .center) {
Button(action: start) {
Text("Open")
}
NavigationLink(destination: ViewB()) {
Text("Change language")
}
}
.sheet(isPresented: $presenting) {
DemoView(code: self.langugage ?? "SE") { result in }
}
}
}
这就是我在 ViewB 上的进展:
var body: some View {
List(langs) { item in
HStack {
NavigationLink(destination: ViewA(presentingSuccessScreen: self.$isPresentingSuccessScreen, language: item.code)) {
Image(item.icon).resizable().frame(width: 40, height: 40)
Text(item.market)
}.frame(height: 64)
}
.navigationBarTitle(Text("Select Language"), displayMode: .inline)
.navigationBarBackButtonHidden(true)
.navigationBarItems(trailing:
Button("Cancel") {
self.presentationMode.wrappedValue.dismiss()
})
}
}
我希望在每一行的右侧都有“>”图像,您可以使用 NavigationList 自动获得它,我希望整行都可以 select。但是通过这种方式,我得到的只是推到堆栈顶部的目标视图,所以如果我多次更改语言,我最终会看到很多视图相互叠加。
任何帮助将不胜感激!
NavigationLink
只在一个方向有效。相反,你需要像
这样的东西
HStack {
VStack {
Image(item.icon).resizable().frame(width: 40, height: 40)
Text(item.market)
}.frame(height: 64)
.onTapGesture {
// set item.code & success flag to some view model and then
// just call dismiss to return to ViewA
self.presentationMode.wrappedValue.dismiss()
}
}
我正在通过构建一个具有 "Settings-View" 的应用程序来测试 SwiftUI,我们称它为 ViewB。此视图有一个列表,您可以在其中 select 一种语言。 第一个视图,ViewA 有 2 个按钮 "Open" 或 "Select language".
我想做的是,从ViewA开始,打开ViewB,select一种语言,自动回到ViewA。这就是我所做的:
视图A
var body: some View {
NavigationView {
VStack(alignment: .center) {
Button(action: start) {
Text("Open")
}
NavigationLink(destination: ViewB()) {
Text("Change language")
}
}
.sheet(isPresented: $presenting) {
DemoView(code: self.langugage ?? "SE") { result in }
}
}
}
这就是我在 ViewB 上的进展:
var body: some View {
List(langs) { item in
HStack {
NavigationLink(destination: ViewA(presentingSuccessScreen: self.$isPresentingSuccessScreen, language: item.code)) {
Image(item.icon).resizable().frame(width: 40, height: 40)
Text(item.market)
}.frame(height: 64)
}
.navigationBarTitle(Text("Select Language"), displayMode: .inline)
.navigationBarBackButtonHidden(true)
.navigationBarItems(trailing:
Button("Cancel") {
self.presentationMode.wrappedValue.dismiss()
})
}
}
我希望在每一行的右侧都有“>”图像,您可以使用 NavigationList 自动获得它,我希望整行都可以 select。但是通过这种方式,我得到的只是推到堆栈顶部的目标视图,所以如果我多次更改语言,我最终会看到很多视图相互叠加。
任何帮助将不胜感激!
NavigationLink
只在一个方向有效。相反,你需要像
HStack {
VStack {
Image(item.icon).resizable().frame(width: 40, height: 40)
Text(item.market)
}.frame(height: 64)
.onTapGesture {
// set item.code & success flag to some view model and then
// just call dismiss to return to ViewA
self.presentationMode.wrappedValue.dismiss()
}
}