【发布时间】:2022-11-26 19:56:31
【问题描述】:
我正在尝试监听布尔值的变化并在听到它成功后更改视图,但是会导致黄色三角形。我还没有设法查明问题,但它似乎与它正在过渡到的观点没有任何关系,因为即使更改错误仍然存在。
我的代码如下
import SwiftUI
struct ConversationsView: View {
@State var isShowingNewMessageView = false
@State var showChat = false
@State var root = [Root]()
var body: some View {
NavigationStack(path: $root) {
ZStack(alignment: .bottomTrailing) {
ScrollView {
LazyVStack {
ForEach(0..<20) { _ in
Text("Test")
}
}
}.padding()
}
Button {
self.isShowingNewMessageView.toggle()
} label: {
Image(systemName: "plus.message.fill")
.resizable()
.renderingMode(.template)
.frame(width: 48, height: 48)
.padding()
.foregroundColor(Color.blue)
.sheet(isPresented: $isShowingNewMessageView, content: {
NewMessageView(show: $isShowingNewMessageView, startChat: $showChat)
})
}
}
.onChange(of: showChat) { newValue in
guard newValue else {return}
root.append(.profile)
}.navigationDestination(for: Root.self) { navigation in
switch navigation {
case .profile:
ChatView()
}
}
}
enum Root {
case profile
}
}
聊天视图()代码:
import SwiftUI
struct ChatView: View {
@State var messageText: String = ""
var body: some View {
VStack {
ScrollView {
VStack(alignment: .leading, spacing: 12) {
ForEach(MOCK_MESSAGES) { message in
MessageView(message: message)
}
}
}.padding(.top)
MessageInputView(messageText: $messageText)
.padding()
}
}
}
非常感谢任何支持。
【问题讨论】:
-
问题说什么?
-
@MrDeveloper 没问题,它只是将视图切换到黄色警告三角形。
-
那么,问题一定出在您的“ChatView()”中 - 请提供该视图的代码。
-
@bjorn.lau 用代码编辑!
-
我非常相信问题出在
navigationDestination修饰符位置,它应该在 NavigationStack.NavigationStack 内,当它找不到合适的路径视图时总是显示。请检查我的答案