SwiftUI: как мне вернуться к тому же индексу с помощью средства выбора, используя SegmentedPickerStyle () - PullRequest
0 голосов
/ 22 марта 2020

Это только я обнимаю ViewBuilders и новую парадигму SwiftUI.

У меня есть «меню» вверху экрана вместе с парой кнопок.

Menu

Если я выполняю поиск (нажимаю на увеличительное стекло), когда я возвращаюсь, индекс всегда возвращается к 0, и первый элемент выбирается / отображается. Я хочу вернуться к тому же индексу, в котором он был при вызове. Как запомнить индекс и сбросить его?

Вот главное меню:

struct TopLevelMenu: View {

/// Toggle to display the Game State (Map) on button tap
@State private var shouldShowWorldMap = false

var body: some View {
NavigationView {
    VStack {
        if shouldShowWorldMap {
            ZStack {
                AnimatedSequence()
                    .modifier(SystemServices())
            } else {
                TopLevelView()

            }
        }
     }
  }
}

struct TopLevelView: View {
   /// Tracks the sheet presentation and current play environment (continent)
    /// mapCenter, display flags, gameOver flat, current continent
    var gameState: GameState = GameState.shared
    /// Handles the game logic, turns, scoring, etc.
    var gameManager: GameManager = GameManager.shared
    /// current game modes: continent, country, capital, grid, about
    @State private var continentIndex = 0

    /// If Help or the World Map is not displayed (triggered by buttons at the top), then this is the Main Display after launch

var body: some View {
    VStack {
        Section {
            Picker(selection: $continentIndex, label: Text("N/A")) {
                ForEach(0 ..< 5) {
                    Text(Continent.continents[$0]).tag($0)
                }
            }
            .pickerStyle(SegmentedPickerStyle())
        }
        SecondLevelView(continentIndex: continentIndex)
            .modifier(SystemServices())
    }
}

}

Обычно я писал бы код UIKit, чтобы сохранить индекс и восстановить его. , но я не уверен, где такой код будет go, так как ViewBuilders не совместимы с встроенным кодом. Что такое принятая практика?

...