在我的应用程序中,我想以编程方式从标签栏页面打开聊天页面。我正在尝试更改选择并安装它。如果聊天行出现在屏幕上,则它可以正常工作并加载。但是如果聊天行在较低的行(例如,如果它在第 100 行),则不会加载。它需要向下滚动并出现加载。我们该如何解决这个问题,如何加载聊天页面?
struct TabPage: View {
@State private var selectedItemIndex : Int = 2
@State var conversNavLink: String? = nil
@State var isActive1: Bool = false
var body: some View {
ZStack{
NavigationView{
VStack(spacing:0) {
switch selectedItemIndex {
case 0: Shop()
case 1: ConversationsView(conversNavLink: $conversNavLink)
case 2: GameHome(isActiveNavLink: $isActive1)
default: Shop()
}
Tabbar(selectedItemIndex: $selectedItemIndex)
}
}
}
.navigationBarHidden(true)
.navigationViewStyle(StackNavigationViewStyle())
.onReceive(NotificationCenter.default.publisher(for: NSNotification.loadChatWithID)){ obj in
if let userInfo = obj.userInfo, let id = userInfo["id"] as? String {
isActive1 = false
selectedItemIndex = 1
conversNavLink = id
}
}
}
}
struct ConversationsView: View {
@EnvironmentObject var manager : ConversationsManager
@EnvironmentObject var um : UserManager
@Binding var conversNavLink: String?
var body: some View {
ZStack{
VStack(spacing:0){
navBar()
ScrollView {
LazyVStack(spacing:0) {
ForEach(manager.convers.sorted(by: { $0.messages.last?.sentDate ?? Date() > $1.messages.last?.sentDate ?? Date()}), id: \.conversID) { convers in
NavigationLink(destination:ChatPage(privateSetting:(um.getUser(curUserID).privateSetting!)).environmentObject(convers), tag: convers.conversID, selection: $conversNavLink) {
ConversationCell(isSelected: self.selections.contains{ $0.conversID == convers.conversID }, isEditing: isEditing){
}
}
}
}
}
}
}
}
}
struct GameHome: View {
@Binding var isActive1: Bool
var body: some View {
ZStack{
NavigationLink(destination: Sub1($isActive1), isActive: $isActive1) {
Text("load sub 1")
}
}
}
}
struct Sub1: View {
@Binding var isActive1: Bool
var body: some View {
ZStack{
NavigationLink(destination: Sub2($isActive1), isActive: $isActive1) {
Text("load sub 2")
}
}
}
}
struct Sub2: View {
@Binding var isActive1: Bool
var body: some View {
ZStack{
BUtton({
NotificationCenter.default.post(name: NSNotification.loadChatWithID,
object: nil, userInfo: ["id": chatID])
}){
Text("load chat with id")
}
}
}
}