在 SwiftUI 中使用函数创建视图时如何使用@Binding?
How to use @Binding when view is creating using function in SwiftUI?
我使用 Function 创建了视图,我正在从另一个视图 (AbcView) 调用它,我想用它执行正常的 @Binding
,但不确定如何传递值并在其中创建 @Binding
功能。
在下面的代码中,我想将 selectedPassengerId
从 AbcView
传递给函数 topSheetClassViews
并在 SelectedTitleView
中的值 passengerIds
更新时执行 @Binding 所以我可以在 AbcView
.
中获得更新值
import SwiftUI
struct AbcView: View {
@StateObject var abcViewModel: AbcViewModel
@State private var selectedPassengerId: Int?
init(accessibiltyID: String, abcViewModel: AbcViewModel) {
self._abcViewModel = StateObject(wrappedValue: abcViewModel)
}
var body: some View {
VStack(spacing: 0) {
// Some Design
}
.overlay(
TopView((accessibilityID: accessibilityID, content: topSheetClassViews(abcViewModel: abcViewModel), selectedRowID: $selectedPassengerId, rowHeight: $rowHeight),, alignment: .top
)
)
}
}
func topSheetClassViews(abcViewModel: AbcViewModel) -> [AnyView] {
var views: [AnyView] = []
for passenger in 0..<abcViewModel.Passengers.count {
views.append(TopSheetPassengerInfoView(abcViewModel: abcViewModel, index: passenger).convertToAnyView())
}
return views
}
struct SelectedTitleView: View {
@ObservedObject var abcViewModel: AbcViewModel
var passengerIds: Int
var body: some View {
VStack(alignment: .trailing) {
Text("passengerIds \(passengerIds)") // here getting correct id which I want to pass to AbcView
Text(abcViewModel.passengerTitle(passengerId: passengerIds))
}
}
}
struct TopSheetPassengerInfoView: View {
@ObservedObject var abcViewModel: AbcViewModel
var index: Int
var body: some View {
VStack(alignment: .leading, spacing: 0) {
Text(abcViewModel.passengers[index].fullName ?? "")
SelectedTitleView(abcViewModel: abcViewModel, passengerIds: Int(abcViewModel.Passengers[index].passengerId ?? "") ?? 0)
}
}
}
只要把它放在你需要传递的地方,比如
这里
func topSheetClassViews(abcViewModel: AbcViewModel, selection: Binding<Int?>) -> [AnyView] {
这里
views.append(TopSheetPassengerInfoView(abcViewModel: abcViewModel, selectedID: selection, index: passenger).convertToAnyView())
这里
struct TopSheetPassengerInfoView: View {
@ObservedObject var abcViewModel: AbcViewModel
@Binding var selectedID: Int?
等等
我使用 Function 创建了视图,我正在从另一个视图 (AbcView) 调用它,我想用它执行正常的 @Binding
,但不确定如何传递值并在其中创建 @Binding
功能。
在下面的代码中,我想将 selectedPassengerId
从 AbcView
传递给函数 topSheetClassViews
并在 SelectedTitleView
中的值 passengerIds
更新时执行 @Binding 所以我可以在 AbcView
.
import SwiftUI
struct AbcView: View {
@StateObject var abcViewModel: AbcViewModel
@State private var selectedPassengerId: Int?
init(accessibiltyID: String, abcViewModel: AbcViewModel) {
self._abcViewModel = StateObject(wrappedValue: abcViewModel)
}
var body: some View {
VStack(spacing: 0) {
// Some Design
}
.overlay(
TopView((accessibilityID: accessibilityID, content: topSheetClassViews(abcViewModel: abcViewModel), selectedRowID: $selectedPassengerId, rowHeight: $rowHeight),, alignment: .top
)
)
}
}
func topSheetClassViews(abcViewModel: AbcViewModel) -> [AnyView] {
var views: [AnyView] = []
for passenger in 0..<abcViewModel.Passengers.count {
views.append(TopSheetPassengerInfoView(abcViewModel: abcViewModel, index: passenger).convertToAnyView())
}
return views
}
struct SelectedTitleView: View {
@ObservedObject var abcViewModel: AbcViewModel
var passengerIds: Int
var body: some View {
VStack(alignment: .trailing) {
Text("passengerIds \(passengerIds)") // here getting correct id which I want to pass to AbcView
Text(abcViewModel.passengerTitle(passengerId: passengerIds))
}
}
}
struct TopSheetPassengerInfoView: View {
@ObservedObject var abcViewModel: AbcViewModel
var index: Int
var body: some View {
VStack(alignment: .leading, spacing: 0) {
Text(abcViewModel.passengers[index].fullName ?? "")
SelectedTitleView(abcViewModel: abcViewModel, passengerIds: Int(abcViewModel.Passengers[index].passengerId ?? "") ?? 0)
}
}
}
只要把它放在你需要传递的地方,比如
这里
func topSheetClassViews(abcViewModel: AbcViewModel, selection: Binding<Int?>) -> [AnyView] {
这里
views.append(TopSheetPassengerInfoView(abcViewModel: abcViewModel, selectedID: selection, index: passenger).convertToAnyView())
这里
struct TopSheetPassengerInfoView: View {
@ObservedObject var abcViewModel: AbcViewModel
@Binding var selectedID: Int?
等等