Linear/Conditional 使用单按钮 swiftUI 导航
Linear/Conditional navigation using a single button swiftUI
我希望能够使用 2 个箭头在视图之间导航,并指示用户所在的页面。
目前,页面指示器有些准确(当我加载到新页面时正确),但是我无法让导航 link 从此结构中工作。
该函数在其自己的文件中,并在另一个视图中使用 Foo(progress: Index)
调用
var Index : Int = 1
struct PageCounter: View {
let progress: Int
init(progress: Int) {
self.progress = progress
}
var body: some View {
HStack {
Button(action: { if Index > 1 { Index -= 1 } } ) {
Image(systemName: "lessthan") }
Spacer()
ForEach(1 ..< 8) {value in
if value-1 < self.progress {
Image(systemName: "circle.fill")
} else {
Image(systemName: "circle")
}
}
Spacer()
NavigationLink(destination: getDestination()) {
Button(action: { if Index < 8 { Index += 1 } } ) {
Image(systemName: "greaterthan") } }
}
}
func getDestination() -> AnyView {
if index == 1 {
return AnyView(View1())
} else if Index == 2 {
return AnyView(View2())
} else if Index == 3 {
return AnyView(View3())
} else if Index == 4 {
return AnyView(View4())
} else {
return AnyView(MainPage())
}
}
}
指示器看起来与此类似 ,但点击箭头目前似乎没有任何反应
我怎样才能让它正常工作?有没有比我目前尝试的更好的方法来实现它?
创建进度和索引结构状态,然后使用带标签的导航视图和使用状态的选择器使其正确导航而不是挂起。
struct PageCounter: View {
@State private var progress: Int?
@State private var index: Int = 1
var body: some View {
HStack {
NavigationLink(destination: getDestination(), tag: index, selection: $progress) {
Button(action: { if index > 1 {
self.progress -= 1
self.progress = self.index } } ) {
Image(systemName: "lessthan") }
制作变量状态并使用导航的标签和选择参数link 允许它根据值自适应更新,而不是在采取行动之前依赖外部刷新
我希望能够使用 2 个箭头在视图之间导航,并指示用户所在的页面。
目前,页面指示器有些准确(当我加载到新页面时正确),但是我无法让导航 link 从此结构中工作。
该函数在其自己的文件中,并在另一个视图中使用 Foo(progress: Index)
var Index : Int = 1
struct PageCounter: View {
let progress: Int
init(progress: Int) {
self.progress = progress
}
var body: some View {
HStack {
Button(action: { if Index > 1 { Index -= 1 } } ) {
Image(systemName: "lessthan") }
Spacer()
ForEach(1 ..< 8) {value in
if value-1 < self.progress {
Image(systemName: "circle.fill")
} else {
Image(systemName: "circle")
}
}
Spacer()
NavigationLink(destination: getDestination()) {
Button(action: { if Index < 8 { Index += 1 } } ) {
Image(systemName: "greaterthan") } }
}
}
func getDestination() -> AnyView {
if index == 1 {
return AnyView(View1())
} else if Index == 2 {
return AnyView(View2())
} else if Index == 3 {
return AnyView(View3())
} else if Index == 4 {
return AnyView(View4())
} else {
return AnyView(MainPage())
}
}
}
指示器看起来与此类似
我怎样才能让它正常工作?有没有比我目前尝试的更好的方法来实现它?
创建进度和索引结构状态,然后使用带标签的导航视图和使用状态的选择器使其正确导航而不是挂起。
struct PageCounter: View {
@State private var progress: Int?
@State private var index: Int = 1
var body: some View {
HStack {
NavigationLink(destination: getDestination(), tag: index, selection: $progress) {
Button(action: { if index > 1 {
self.progress -= 1
self.progress = self.index } } ) {
Image(systemName: "lessthan") }
制作变量状态并使用导航的标签和选择参数link 允许它根据值自适应更新,而不是在采取行动之前依赖外部刷新