Swiftui -Bindung, die beim Empfangen neuer Daten von UIKIT nicht aktualisiert werdenIOS

Programmierung für iOS
Anonymous
 Swiftui -Bindung, die beim Empfangen neuer Daten von UIKIT nicht aktualisiert werden

Post by Anonymous »

In Swiftui habe ich eine Ansicht, die eine UIKIT -Ansicht über UIViewControlleRepresentable .
In dieser Zeile:
verwendet

Code: Select all

daysBinding.wrappedValue = newDays
Ich kann in der Tat sehen, dass NewDays verschiedene Daten zurückgibt, und ich würde erwarten, dass der Wert der lokalen Tage Bindung aktualisiert wird. Es ist jedoch nicht.

Code: Select all

print("🛠 localDays is now:", updated.map { $0.stops.map(\.id) })
< /code>
oder: < /p>
print("🔄 days changed externally, updating localDays")
Was mache ich falsch und wie kann ich sicherstellen, dass lokale Tage aktualisiert werden, wenn NewDays auf der Zeilen -DaysBinding aktualisiert wird.

Code: Select all

import SwiftUI

class ReorderStopsViewModel: ObservableObject {
@Published var isPresented = false
}

struct ReorderStopsView: View {
@Environment(\.dismiss) var dismiss
@EnvironmentObject var tripManager: TripManager

@Binding var days: [Day]
@Binding var trip: Trip

@State private var localDays: [Day] = []

var body: some View {
NavigationStack {
ReorderStopsUIKitWrapper(days: $localDays)
.edgesIgnoringSafeArea(.all)
.navigationTitle("Reorder Stops")
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .confirmationAction) {
Button("Save") {
saveReorderedStops()
dismiss()
}
.font(.custom("Inter-SemiBold", size: 16))
}

ToolbarItem(placement: .cancellationAction) {
Button("Cancel") {
dismiss()
}
.font(.custom("Inter-Regular", size: 16))
}
}
}
.onAppear {
self.localDays = self.days // make a copy on appear
}
.onChange(of: localDays) { updated in
print("🛠 localDays is now:", updated.map { $0.stops.map(\.id) })
}
.onChange(of: days) { newValue in
print("🔄 days changed externally, updating localDays")
self.localDays = newValue
}
}

private func saveReorderedStops() {

// 4) Dismiss
dismiss()
}

}

struct ReorderDayHeader: View {
@Binding var day: Day

var body: some View {
VStack(alignment: .leading) {
Text(day.date.formatDate(fromFormat: "yyyy-MM-dd", toFormat: "EEEE") ?? "")
Text(day.date.formatDate(fromFormat: "yyyy-MM-dd", toFormat: "MMMM d") ?? "")
}
.frame(maxWidth: .infinity, alignment: .leading)
.padding(.vertical, 8)
.padding(.horizontal, 16)
}
}

struct ReorderStopView: View {
@Binding var stop: TripStop

var body: some View {
Text(stop.place?.details?.name ?? "")
.lineLimit(1)
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 50)
.padding(.vertical, 8)
.padding(.horizontal, 16)
.background(Color.wireframe)
}
}

struct ReorderStopsUIKitWrapper: UIViewControllerRepresentable {
@Binding var days: [Day]

func makeCoordinator() -> Coordinator {
Coordinator(days: $days)
}

func makeUIViewController(context: Context) -> UINavigationController {
// Inject the current days plus a callback into the UIKit VC
let reorderVC = ReorderStopsViewController(days: days) { updated in
self.days = updated
context.coordinator.daysUpdated(updated)
}
return UINavigationController(rootViewController: reorderVC)
}

func updateUIViewController(_ uiViewController: UINavigationController, context: Context) {
// This is called when SwiftUI’s `days` changes;  push those into the table
if let vc = uiViewController.topViewController as? ReorderStopsViewController {
vc.days = days
vc.tableView.reloadData()
}
}

class Coordinator {
private var daysBinding: Binding

init(days: Binding) {
self.daysBinding = days
}

/// Called from your ReorderStopsViewController
func daysUpdated(_ newDays: [Day]) {
daysBinding.wrappedValue = newDays
}
}
}

#Preview {
ReorderStopsView(days: .constant([Day.data, Day.data]), trip: .constant(Trip.data))
}

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post