rob*_*ior 5 xcode swift swiftui ios16
我正在尝试监听布尔值的变化,并在听到成功后更改视图,但是会产生黄色三角形。我还没有设法查明问题,但它似乎与它正在转换的视图没有任何关系,因为即使更改后错误仍然存在。
我的代码如下
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
}
}
Run Code Online (Sandbox Code Playgroud)
ChatView() 代码:
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()
}
}
}
Run Code Online (Sandbox Code Playgroud)
非常感谢任何支持。
Evg*_*y K 14
您应该在组件navigationDestination内使用修饰符NavigationStack,只需移动它即可。
NavigationStack(path: $root) {
ZStack(alignment: .bottomTrailing) {
ScrollView {
LazyVStack {
ForEach(0..<20) { _ in
Text("Test")
}
}
}.padding()
}.navigationDestination(for: Root.self) { navigation in
switch navigation {
case .profile:
ChatView()
}
}
//...
}
Run Code Online (Sandbox Code Playgroud)
基本上这个黄色三角形意味着NavigationStack找不到合适的路径组件。当您navigationDestination直接在NavigationStackView 或外部的某个地方使用时,它会被忽略
小智 1
您必须将 .environmentObject(root) 设置为 NavigationStack,以便为视图子层次结构(在您的情况下为 ChatView)提供 NavigationPath。此外,您的 ChatView 中还必须具有 Root 类型的 @EnvironmentObject 属性,以便它可以读取路径。
| 归档时间: |
|
| 查看次数: |
2096 次 |
| 最近记录: |