将对象详细信息传递到另一个视图[SwiftUI]

时间:2020-04-26 11:31:46

标签: swiftui swiftui-navigationlink

面对问题,以了解如何使用NavigationLink将对象详细信息移动到另一个视图,我已经阅读了许多文章并观看了视频,除了Preview结构,它们的工作方式与我相同,它们使用本地数据,并且可以轻松地映射查看数据的第一项,例如data [0]。在我的情况下,我在线获取数据,因此上述方法没有帮助我克服Preview结构的错误:错误:参数缺少参数

已阅读以下文章: developer.apple.com/tutorials/swiftui/building-lists-and-navigation

www.raywenderlich.com/5824937-swiftui-tutorial-navigation

www.youtube.com/watch?v=BCSP_uh0co0&ab_channel=azamsharp

///主视图代码:

    import SwiftUI
    import SDWebImageSwiftUI

    struct HomeView: View {
        @State var posts: [Posts] = []
        @State var intPageNo: Int = 0
        var body: some View {
            NavigationView {
                List(posts) {post in
                    NavigationLink(destination: ViewPostView(post: post)){
                        VStack{
                            HStack{
                                WebImage(url: URL(string: post.featured_image))
                                    .resizable()
                                    .placeholder(Image("logo"))
                                    .frame(width: 150, height: 150, alignment: .center)

                                VStack(alignment: .leading, spacing: 10.0){
                                    Text("By: \(post.author_name)")
                                    Text("Since: \(post.since)")
                                    Text("City: \(post.city_name)")
                                    Text("Category: \(post.category_name)")

                                }
                                .font(.footnote)

                                Spacer()
                            }

                            Text(post.title)
                                .font(.body)
                                .fontWeight(.bold)
                                .frame(alignment: .trailing)
                                .flipsForRightToLeftLayoutDirection(true)
                        }
                    }

                }
                .onAppear{
                    self.intPageNo += 1
                    ApiPosts().getPosts(intPage: self.intPageNo){(posts) in
                        self.posts = posts
                    }
                }

                .navigationBarTitle(Text("Home"))
            }

        }
    }

    struct HomeView_Previews: PreviewProvider {
        static var previews: some View {
            HomeView()
        }
    }

///详细查看代码:

    import SwiftUI

    struct ViewPostView: View {

        @State var comments: [Comments] = []
        @State var post: Posts

        var body: some View {
            NavigationView {
                VStack{
                    Text(post.post_content)
                        .frame(alignment: .trailing)
                    List(comments){comment in
                        VStack(alignment: .trailing, spacing: 10){
                            HStack(spacing: 40){
                                Text(comment.author_name)
                                Text(comment.comment_date)
                            }

                            Text(comment.comment_content)
                        }
                    }
                    .frame(alignment: .trailing)
                    .onAppear {

                        PostViewManager().getComments(intPostID: self.post.id){(comments) in
                            self.comments = comments
                        }
                    }
                }

            }
        }
    }

    struct ViewPostView_Previews: PreviewProvider {
        static var previews: some View {

            ViewPostView()
        }
    }

///正在获取数据代码:

    struct Comments: Codable, Identifiable {
        var id: Int
        var author_name: String
        var comment_content: String
        var comment_date: String
        var comment_date_gmt: String
        var approved: String
    }

    class PostViewManager {


        func getComments(intPostID: Int, completion: @escaping ([Comments]) -> ()){

            guard let url = URL(string: "https://test.matjri.com/wp-json/matjri/v1/comments/\(intPostID)") else {return}

            URLSession.shared.dataTask(with: url) { (data, _, _) in
                let comments = try! JSONDecoder().decode([Comments].self, from: data!)
                DispatchQueue.main.async {
                    completion(comments)
                }
            }
            .resume()
        }

    }

    struct Posts: Codable, Identifiable {
        var id: Int
        var title: String
        var city_id: Int
        var city_name: String
        var category_id: Int
        var category_name: String
        var since: String
        var author_id: String
        var author_name: String
        var post_content: String
        var featured_image: String
    }

    class ApiPosts {

        func getPosts(intPage: Int, completion: @escaping ([Posts]) -> ()){
            guard let url = URL(string: "https://test.matjri.com/wp-json/matjri/v1/posts/0") else {return}

            URLSession.shared.dataTask(with: url) { (data, _, _) in
                let posts = try! JSONDecoder().decode([Posts].self, from: data!)
                DispatchQueue.main.async {
                    completion(posts)
                }
            }
            .resume()
        }
    }

1 个答案:

答案 0 :(得分:1)

您收到“ Preview struct,ERROR:Missing parameter for parameter”的错误,通常是因为您没有向Preview提供所需的参数。

ViewPostView希望传递“ var post:Posts”,因此在ViewPostView_Previews中可以 需要提供该信息,例如:

    struct ViewPostView_Previews: PreviewProvider {
    static var previews: some View {
        ViewPostView(post: Posts(id: 1, title: "title", ... ))
    }
}