我的应用程序上有一个 NavigationView。从 mainView 我使用 NavigationLink MessagesView 打开,从 MessagesView 内部我使用 NavigationView ChatView 打开。在 ChatView 上,我制作了一个 backButton,但是当我按下 backButton 时,我不知道如何让 MessagesView 重新加载。
我在网上搜索了解决方案,但要么不起作用,要么我不明白。
这是我到目前为止的代码:
struct MessagesView: View {
@ObservedObject var allMessages = GetAllMessages()
@State var readNewThreadMessages = [MessagesDetailes]()
//astea sunt puse pentru a scapa de bugul ca nu iti incarca cateodata View
@ObservedObject var observer = Observer()
@Binding var incepemOnReceive : Bool
var body: some View {
ScrollView(.vertical, showsIndicators: false) {
if allMessages.readThreadMessages.count == 0 {
LoadingView()
.padding(.top, 50)
} else {
//mesajele din DB
ForEach(allMessages.readThreadMessages) { message in
NavigationLink(destination: ChatView(incepemOnReceive: $incepemOnReceive, other_name: message.other_user, other_object: message.other_object, produs_nume: message.product_name, produs_pret: message.product_price, produs_id: message.product_id, thread_obj: message.threadObj, readThreadMessages: $allMessages.readThreadMessages).onAppear(perform: {
self.allMessages.markMessageAsSeen(obiect: message.readStateObj, isRead: message.seen)
})) {
MessagesRow(message: message)
.background(message.seen ? Color.clear : Color("ColorSecondary").opacity(0.1))
}
}
}
}
.onReceive(self.observer.$enteredForeground){ _ in
if self.incepemOnReceive {
self.allMessages.readAllMessages()
print("read from db")
}
}
.onAppear(perform: {
self.incepemOnReceive = true
})
.navigationTitle(LocalizedStringKey("MessagespNavT"))
.navigationBarTitleDisplayMode(.inline)
}}
和带有 backButton 的 ChatView
struct ChatView: View {
@Environment(\.presentationMode) var presentationMode
@ObservedObject var getChatMessages = GetChatMessages()
@Binding var incepemOnReceive : Bool
var other_name : String
var other_object : PFObject
var produs_nume : String
var produs_pret : String
var produs_id : String
var thread_obj : PFObject
@State var writeMessage = ""
@State var messageArray = [CurrentChatMessages]()
@State var showFromDB = true
@Binding var readThreadMessages : [MessagesDetailes]
@State var size : CGFloat = 35
//photo picker
@State var isPresented: Bool = false
@State var pickerResult: [UIImage] = []
var config: PHPickerConfiguration {
var config = PHPickerConfiguration(photoLibrary: PHPhotoLibrary.shared())
config.filter = .images //videos
config.selectionLimit = 1
return config
}
var body: some View {
VStack{
// show chat
}.onAppear(perform: {
self.incepemOnReceive = false
UITextView.appearance().backgroundColor = UIColor(.gray).withAlphaComponent(0.15)
self.getChatMessages.getAllChatMessages(threadObj: thread_obj)
})
.navigationBarItems(leading:
Button(action: {
for mesaj in 0..<readThreadMessages.count-1 {
if readThreadMessages[mesaj].threadObj == thread_obj {
readThreadMessages[mesaj].seen = true
}
}
self.incepemOnReceive = true
self.presentationMode.wrappedValue.dismiss()
}) {
CustomBackButton()
})
}
}