SwiftUI TabView: установить выбранный TabItem из другого представления, но обнаруживающий повторный выбор

#button #swiftui #controls #tabview

#кнопка #swiftui #элементы управления #tabview

Вопрос:

Я хотел бы отобразить TabView для отображения разных экранов. На моем первом экране (home) отображаются три кнопки, которые могут выбирать один из трех экранов для отображения. Но (!) Я должен проверить, неоднократно ли выбирается вкладка, чтобы вызвать в этом случае специальное действие.

Обнаружение повторного выбора для screen2 работает нормально, но тогда я не могу установить выбор с помощью кнопок. Я пытался использовать @EnvironmentalObject , но изменения в этом объекте не наблюдаются в моем TabView.selection.

    import SwiftUI

   @main
   struct TestApp: App {
       static let kIndex0 = 0
       static let kIndex1 = 1
       static let kIndex2 = 2

       var appState = AppState()

       @State private var selection = TestApp.kIndex0

       var body: some Scene {
           // this code is required to detect a repeated selection of
           // the same tab to trigger a special action
           let index = Binding<Int>(
               get: { self.selection },
               set: {
                   if $0 == TestApp.kIndex1  amp;amp;  self.selection == $0 {
                       print("Trigger special action for index 1")
                   }
                   print("Pressed tab: ($0)  self.selction: (self.selection)  app.selectedTab: (appState.selectedTab)")

                   self.selection = $0
                   appState.selectedTab = $0
               })

           WindowGroup {
               TabView(selection: index) {
                   First()
                       .environmentObject(appState)
                      .tabItem {
                           Image(systemName: "1.circle")
                           Text("Home")
                       }.tag(TestApp.kIndex0)
                
                   Text("Second Content View")
                       .tabItem {
                           Image(systemName: "2.circle")
                           Text("Screen Two")
                       }.tag(TestApp.kIndex1)

                   Text("Third Content View")
                       .tabItem {
                           Image(systemName: "3.circle")
                           Text("Screen Three")
                       }.tag(TestApp.kIndex2)
               }
           }
       }
   }


   class AppState: ObservableObject {
       @Published var selectedTab = TestApp.kIndex0
   }


   /*
       Place to buttons which should select on of the the two
       tabs of TestUI
    */
   struct First: View {
       @EnvironmentObject var appState: AppState

       var body: some View {
           VStack(alignment: .leading, spacing: 20) {
               AButton(tabIndex: TestApp.kIndex0, iconName: "1.circle", text: "This first screen")
                   .environmentObject(appState)

               AButton(tabIndex: TestApp.kIndex1, iconName: "2.circle", text: "Second screen")
                   .environmentObject(appState)

                  AButton(tabIndex: TestApp.kIndex1, iconName: "3.circle", text: "Third screen")
                   .environmentObject(appState)
           }
       }
   }


   struct AButton: View {
       let tabIndex: Int
       let iconName: String
       let text: String

       @EnvironmentObject var appState: AppState

       var body: some View {
           Button(action: {
               appState.selectedTab = tabIndex
           }) {
               HStack() {
                   Image(systemName: iconName)
                       .imageScale(.large)
                       .frame(minWidth: 50)
                   Text(text)
               }
           }
       }
   }
  

Ответ №1:

Вам нужно сделать так, чтобы appState наблюдалось, и вам вообще не нужно selection (это просто дубликат).

Я поместил все в разделенные ContentView (чтобы оставить сцену только для сцены)

Протестировано с Xcode 12 / iOS 14

 struct ContentView: View {

    @StateObject var appState = AppState()

    var body: some View {
        // this code is required to detect a repeated selection of
        // the same tab to trigger a special action
        let index = Binding<Int>(
            get: { self.appState.selectedTab },
            set: {
                if $0 == TestApp.kIndex1  amp;amp;  self.appState.selectedTab == $0 {
                    print("Trigger special action for index 1")
                }
                print("Pressed tab: ($0) app.selectedTab: (appState.selectedTab)")
                appState.selectedTab = $0
            })

        TabView(selection: index) {
            First()
                .environmentObject(appState)
                .tabItem {
                    Image(systemName: "1.circle")
                    Text("Home")
                }.tag(TestApp.kIndex0)

            Text("Second Content View")
                .tabItem {
                    Image(systemName: "2.circle")
                    Text("Screen Two")
                }.tag(TestApp.kIndex1)

            Text("Third Content View")
                .tabItem {
                    Image(systemName: "3.circle")
                    Text("Screen Three")
                }.tag(TestApp.kIndex2)
        }
    }
}


class AppState: ObservableObject {
    @Published var selectedTab = TestApp.kIndex0
}


struct First: View {
    @EnvironmentObject var appState: AppState

    var body: some View {
        VStack(alignment: .leading, spacing: 20) {
            AButton(tabIndex: TestApp.kIndex0, iconName: "1.circle", text: "This first screen")

            AButton(tabIndex: TestApp.kIndex1, iconName: "2.circle", text: "Second screen")

            AButton(tabIndex: TestApp.kIndex2, iconName: "3.circle", text: "Third screen")
        }
    }
}


struct AButton: View {
    let tabIndex: Int
    let iconName: String
    let text: String

    @EnvironmentObject var appState: AppState

    var body: some View {
        Button(action: {
            appState.selectedTab = tabIndex
        }) {
            HStack() {
                Image(systemName: iconName)
                    .imageScale(.large)
                    .frame(minWidth: 50)
                Text(text)
            }
        }
    }
}
  

Комментарии:

1. Спасибо, работает также в моем XCode (12 beta 6, iOS14). Я думал, что попробовал аналогичное решение, но теперь я не уверен. Спасибо за вашу помощь.