【问题标题】:SwiftUI - Switching views with NavigationViewSwiftUI - 使用 NavigationView 切换视图
【发布时间】:2021-09-15 18:30:46
【问题描述】:

我正在尝试将 cmets 添加到我的应用程序中。为此,我显然需要切换我的应用程序的视图。目前,我有两个文件:PostView 和 PostRow。 PostView 是实际的视图文件,而 PostRow 是显示帖子的文件。

我正在尝试在 PostRow 中创建一个按钮,它将视图从 PostView 切换到 CommentView。但是,如果我只是将 NavigationView 添加到 PostRow,我的帖子不会加载。如果我将 NavigationView 添加到我的 PostView 文档中,我会得到大量的视觉故障。

我不知道如何使用此文档结构正确切换视图。

PostView.swift

import SwiftUI

struct PostView: View {
    var edges = UIApplication.shared.windows.first?.safeAreaInsets
    @StateObject var postData = PostViewModel()
    var body: some View {
        
        VStack{
            HStack{
                
                Text("Posts")
                    .font(.largeTitle)
                    .fontWeight(.heavy)
                .foregroundColor(.white)
            
                Spacer(minLength: 0)
            .padding(.top,edges!.top)
            //shadow
            .background(Color("bg"))
            .shadow(color: Color.white.opacity(0.06), radius: 5, x: 0, y: 5)
            
            if postData.posts.isEmpty{
                
                Spacer(minLength: 0)
                
                if postData.noPosts{
                    
                    Text("No Posts")
                }
                else{
                    ProgressView()
                }
                Spacer(minLength: 0)
            }
            else{
                ScrollView{
                    VStack(spacing: 15){
                        
                        ForEach(postData.posts){post in
                            
                            PostRow(post: post, postData: postData)
                        }
                    }
                    .padding()
                    .padding(.bottom,55)
                }
            }
        }
        .fullScreenCover(isPresented: $postData.newPost) {
            
            NewPost(updateId: $postData.updateId)
            }
        }
        
    }

PostRow.swift - 带有按钮的文档的一部分

HStack {
                Text(post.title)
                    .fontWeight(.bold)
                    .foregroundColor(.white)
                    .fixedSize(horizontal: false, vertical: true)
                
                Spacer(minLength: 0)
            }
            .padding(.top,5)
            .padding(.bottom,5)
            
            HStack {
                Spacer(minLength: 0)
                Text(post.time,style: .time)
                    .font(.caption2)
                    .fontWeight(.light)
                    .foregroundColor(.white)
            }
Button("Comments") {
                  //This is where I need to switch to the CommentsView  
                }

我相信这是一个相对简单的问题,但我似乎无法弄清楚。

谢谢!

【问题讨论】:

    标签: swift xcode swiftui navigationview


    【解决方案1】:

    有很多方法可以做到这一点。这是一个。

    import SwiftUI
    enum MainViewTypes: String{
        case comments
        case posts
    }
    struct ParentView: View {
        @SceneStorage("mainViewType") var mainViewType: String = MainViewTypes.posts.rawValue
        var body: some View {
            switch mainViewType{
            case MainViewTypes.comments.rawValue:
                //Comments View
                VStack{
                    Text("comments")
                    Button("go to posts", action: {
                        mainViewType = MainViewTypes.posts.rawValue
                    })
                }
            case MainViewTypes.posts.rawValue:
                //Posts view
                VStack{
                    Text("posts")
                    PostRowView()
                }
            default:
                Button("unknown", action: {
                    mainViewType = MainViewTypes.posts.rawValue
                })
            }
        }
    }
    struct PostRowView: View {
        @SceneStorage("mainViewType") var mainViewType: String = MainViewTypes.posts.rawValue
        var body: some View {
            Button("go to comments", action: {
                mainViewType = MainViewTypes.comments.rawValue
            })
        }
    }
    struct ParentView_Previews: PreviewProvider {
        static var previews: some View {
            ParentView()
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-09
      • 1970-01-01
      • 2020-08-29
      • 2021-02-04
      • 2022-01-26
      相关资源
      最近更新 更多