从另一个视图接收选定的TabView-SwiftUI

时间:2020-07-24 13:22:25

标签: swiftui xcode11 tabview

我正在尝试使用tabView制作swiftui应用。 我希望tabview正常工作,但是所选的标签可能来自第一页 第一个视图

struct ContentView: View {
       @State  var selectedTab = 0

       var body: some View {
           VStack{
               NavigationView{
                   VStack(alignment: .center, spacing: 0){
                       Spacer()
                       NavigationLink(destination: AccueilView(selectedTab: self.$selectedTab)){
                           VStack{
                               Image(systemName: "book")
                               Text("Enseignements")
                           }
                        }
                       HStack{
                           NavigationLink(destination: AccueilView(selectedTab: self.$selectedTab)){
                               VStack{
                                   Image(systemName: "list.dash")
                                   Text("Étapes")
                               }
                           }
                           Image(systemName: "map")
                               .resizable()
                               .frame(width: 150, height: 150, alignment: .center)
                           NavigationLink(destination: AccueilView(selectedTab: self.$selectedTab)){
                               VStack{
                                   Image(systemName: "photo.on.rectangle")
                                   Text("Album")
                               }
                           }
                       }
                       NavigationLink(destination: AccueilView(selectedTab: self.$selectedTab)){
                           VStack{
                               Image(systemName: "square.stack.3d.down.right")
                               Text("Chroniques")
                           }
                       }
                       Spacer()
                       
                   }
                   .edgesIgnoringSafeArea(.bottom)
                   
                       
               }
           }
        }
}

第二个视图

struct AccueilView: View {
    @Binding  var selectedTab: Int
     
     var body: some View {
         TabView(selection: $selectedTab) {
             EtapeView(card: Card.example)
                 .tabItem {
                     Image(systemName: "list.dash")
                     Text("Étapes")
             }
             .tag(0)
             AlbumView()
                 .tabItem {
                     Image(systemName: "photo.on.rectangle")
                     Text("Album")
             }
             .tag(1)
             TeachingView(card: Card.example)
                 .tabItem{
                     Image(systemName: "book")
                     Text("Enseignements")
             }
             .tag(2)
             ChronicView(card: Card.example)
                 .tabItem{
                     Image(systemName: "square.stack.3d.down.right")
                     Text("Chroniques")
             }.tag(3)
         }
     }
}

并且我希望ContentView将selectedTab传递给AccueilView,而AccueilView不要更改tabView的正常状态。 例如:如果我在ContenView中单击“专辑”,则可以直接进入AccueilView等的“相册”中,而从“相册”中,我可以进入例如年代记。 谢谢你的帮助

1 个答案:

答案 0 :(得分:1)

如果我正确理解了您的目标,这是可行的方法。

通过Xcode 12 / iOS 14测试

demo

修改后的代码:

struct TestNavigateToTab: View {
    var body: some View {
        VStack{
            NavigationView{
                VStack(alignment: .center, spacing: 0){
                    Spacer()
                    NavigationLink(destination: AccueilView(selectedTab: 2)){
                        VStack{
                            Image(systemName: "book")
                            Text("Enseignements")
                        }
                    }
                    HStack{
                        NavigationLink(destination: AccueilView(selectedTab: 0)){
                            VStack{
                                Image(systemName: "list.dash")
                                Text("Étapes")
                            }
                        }
                        Image(systemName: "map")
                            .resizable()
                            .frame(width: 150, height: 150, alignment: .center)
                        NavigationLink(destination: AccueilView(selectedTab: 1)){
                            VStack{
                                Image(systemName: "photo.on.rectangle")
                                Text("Album")
                            }
                        }
                    }
                    NavigationLink(destination: AccueilView(selectedTab: 3)){
                        VStack{
                            Image(systemName: "square.stack.3d.down.right")
                            Text("Chroniques")
                        }
                    }
                    Spacer()
                    
                }
                .edgesIgnoringSafeArea(.bottom)
            }
        }
    }
}

struct AccueilView: View {
    @State var selectedTab: Int
    
    init(selectedTab: Int) {
        _selectedTab = State(initialValue: selectedTab)
    }
    
    var body: some View {
        TabView(selection: $selectedTab) {
            Text("EtapeView")
                .tabItem {
                    Image(systemName: "list.dash")
                    Text("Étapes")
            }
            .tag(0)
            Text("AlbumView")
                .tabItem {
                    Image(systemName: "photo.on.rectangle")
                    Text("Album")
            }
            .tag(1)
            Text("TeachingView")
                .tabItem{
                    Image(systemName: "book")
                    Text("Enseignements")
            }
            .tag(2)
            Text("ChronicView")
                .tabItem{
                    Image(systemName: "square.stack.3d.down.right")
                    Text("Chroniques")
            }.tag(3)
        }
    }
}