0

我希望能够使用 2 个箭头在视图之间导航,并指示用户在哪个页面上。

目前,页面指示器有些准确(当我加载到新页面时正确),但是我无法让导航链接在这个结构中工作。

该函数位于其自己的文件中,并在另一个视图中使用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())
        }
      }
    }

指示器看起来与此类似,但是在此处输入图像描述单击箭头当前似乎没有做任何事情

我怎样才能让它正常工作?有没有比我目前尝试的更好的方法来实现它?

4

1 回答 1

0

制作进度和索引结构状态,然后使用带有标签的导航视图和使用状态的选择器使其正确导航而不是挂起。

    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") } 

使变量状态并使用导航链接的标记和选择参数允许它根据值自适应更新,而不是在采取行动之前依赖外部刷新

于 2020-10-12T13:19:40.240 回答