【问题标题】:SwiftUI - Picker doesn't updateSwiftUI - 选择器不更新
【发布时间】:2021-05-03 11:53:45
【问题描述】:

我有一个小问题,

我有一个带有选择器的 Swift 应用:

/// DATA.swift

import SwiftUI

class FormPage: ObservableObject { 
    @State var country = ["USA", "Japan", "Germany"]
    @State var index = 0
}

/// FORM.swift

import SwiftUI

struct ContentView: View {
    
    @StateObject var choiceForm = FormForFormulaire()
    
    var body: some View {
        
        Form {
            Section {
                Picker(selection: $choiceForm.index, label: Text("Pays de destination")) {
                    ForEach(0 ..< choice.country.count) {
                        Text(choiceForm.country[$0]).tag($0)
                    }
                }
            }
        }

选择器视图正常,但没有更新:我点击日本,视图没有改变。

对不起我的英语,我希望有人能理解我!

谢谢

【问题讨论】:

    标签: ios swift swiftui


    【解决方案1】:

    FormPage 类中使用@Published 作为索引

    @Published var index = 0
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多