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

Receive the selected TabView from another view - SwiftUI

我正在尝试使用 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 中单击“Album”,我会直接进入 AccueilView 中的 Album 等,例如,我可以从 Album 转到 chronique。 谢谢你的帮助

如果我正确理解了你的目标,这里是可能的方法。

测试 Xcode 12 / iOS 14

修改后的代码:

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