Программируем под IOS
-
Anonymous
Как инициализировать базу свойств привязки на значениях из ViewModel @ObServable?
Сообщение
Anonymous »
У меня есть простое представление:
в коде.
Код: Выделить всё
@State var selectedOption: Option?
init() {
print(viewModel.options.count)
selectedOption = viewModel.options.first { $0.identifier == Settings.currentLanguage }
print(selectedOption)
print(viewModel.options.first { $0.identifier == Settings.currentLanguage })
print(viewModel.options.map { $0.identifier })
print(Settings.currentLanguage)
}
var body: some View {
ZStack {
ScrollView {
VStack(spacing: 40) {
InAppPurchaseView()
HStack {
TextLabel("settings.language".localized)
Spacer()
VStack(alignment: .leading) {
TextLabel(selectedOption?.title, size: 24, weight: .bold)
TextLabel(selectedOption?.value)
}
}
.onTapGesture {
isSelected = true
}
}
}
.padding(20)
BottomSheetView(isShowing: $isSelected) {
PickerSelect(selectedOption: $selectedOption, options: viewModel.options)
}
}
}
и после того, как пользователь нажмите «Просмотр», чтобы изменить язык:
Подробнее здесь:
https://stackoverflow.com/questions/795 ... observable
1743441225
Anonymous
У меня есть простое представление:
в коде.[code]@State var selectedOption: Option?
init() {
print(viewModel.options.count)
selectedOption = viewModel.options.first { $0.identifier == Settings.currentLanguage }
print(selectedOption)
print(viewModel.options.first { $0.identifier == Settings.currentLanguage })
print(viewModel.options.map { $0.identifier })
print(Settings.currentLanguage)
}
var body: some View {
ZStack {
ScrollView {
VStack(spacing: 40) {
InAppPurchaseView()
HStack {
TextLabel("settings.language".localized)
Spacer()
VStack(alignment: .leading) {
TextLabel(selectedOption?.title, size: 24, weight: .bold)
TextLabel(selectedOption?.value)
}
}
.onTapGesture {
isSelected = true
}
}
}
.padding(20)
BottomSheetView(isShowing: $isSelected) {
PickerSelect(selectedOption: $selectedOption, options: viewModel.options)
}
}
}
[/code]
и после того, как пользователь нажмите «Просмотр», чтобы изменить язык:
Подробнее здесь: [url]https://stackoverflow.com/questions/79546821/how-to-initialize-binding-property-base-on-values-from-viewmodel-observable[/url]