如何将完整数据传递到另一个视图

时间:2020-03-26 18:26:13

标签: swift swiftui

因此,使用此行:

 .onReceive(model2.$postsById) { postById in
                    print("Model2 \(postById)")

                    }

我得到了这些数据:

Model2 [Test.PostById(id: "9ffb2c83-45ed-4198-b6ab-9a259ef91b44", album_id: "9ffb2c83-45ed-4198-b6ab-9a259ef91b44", name: nil, path: "public/1584106997200-astronomy-space-abstract-galaxy.jpg"), Test.PostById(id: "7ac7ff5f-4b4b-458f-b9a6-491813dbe97d", album_id: "9ffb2c83-45ed-4198-b6ab-9a259ef91b44", name: Optional("California Wind "), path: "public/1584106997230-California_Wind.mp3"), Test.PostById(id: "d804c511-fdd6-453c-816d-fbe501b0ba8d", album_id: "9ffb2c83-45ed-4198-b6ab-9a259ef91b44", name: Optional("Cosmic Love "), path: "public/1584106997710-Cosmic_Love.mp3"), Test.PostById(id: "094e5b65-f0b2-4a5d-8c20-c6fa3d733325", album_id: "9ffb2c83-45ed-4198-b6ab-9a259ef91b44", name: Optional("Happy Sixth "), path: "public/1584106997890-Happy_Sixth.mp3"), Test.PostById(id: "f148fd8e-6896-492b-8e5a-c99072cad562", album_id: "9ffb2c83-45ed-4198-b6ab-9a259ef91b44", name: Optional("How it Began "), path: "public/1584106998000-How_it_Began.mp3")]
Model2 [Test.PostById(id: "7c35e1ee-28e0-4845-bd14-dcad27025b04", album_id: "7c35e1ee-28e0-4845-bd14-dcad27025b04", name: nil, path: "public/1584039156755-add.png"), Test.PostById(id: "c311604f-0bfc-4930-bfdf-fcf5592483b9", album_id: "7c35e1ee-28e0-4845-bd14-dcad27025b04", name: Optional("A Typical Ride Out "), path: "public/1584039156755-A_Typical_Ride_Out.mp3"), Test.PostById(id: "984857d1-5d39-4e66-9379-e8346f7e5b43", album_id: "7c35e1ee-28e0-4845-bd14-dcad27025b04", name: Optional("Because For Everything There Is Someone "), path: "public/1584039156941-Because_For_Everything_There_Is_Someone.mp3"), Test.PostById(id: "1919b016-6a6e-4793-8305-71207d9518ae", album_id: "7c35e1ee-28e0-4845-bd14-dcad27025b04", name: Optional("California Wind "), path: "public/1584039157156-California_Wind.mp3")]
Model2 [Test.PostById(id: "f1567e48-eb24-4ccc-b24d-6c7c9c99e326", album_id: "f1567e48-eb24-4ccc-b24d-6c7c9c99e326", name: nil, path: "public/1584038265741-Profile-img-icon.png"), Test.PostById(id: "4e9d5c31-d48e-4d0a-8f26-ba67d1ac39c2", album_id: "f1567e48-eb24-4ccc-b24d-6c7c9c99e326", name: Optional("Prophet 7 "), path: "public/1584038265742-Prophet_7.mp3"), Test.PostById(id: "71b80407-2434-445e-8e0b-30e1a45293a7", album_id: "f1567e48-eb24-4ccc-b24d-6c7c9c99e326", name: Optional("Rubber Ducky "), path: "public/1584038265907-Rubber_Ducky.mp3"), Test.PostById(id: "0069ba6a-85c2-48d8-b5bd-d0e70c84bea2", album_id: "f1567e48-eb24-4ccc-b24d-6c7c9c99e326", name: Optional("Shameless Life Wisdom Sendoff "), path: "public/1584038265996-Shameless_Life_Wisdom_Sendoff.mp3")]
Model2 [Test.PostById(id: "57e19ce4-ef2e-499e-aa76-b3e32ff80b4e", album_id: "57e19ce4-ef2e-499e-aa76-b3e32ff80b4e", name: nil, path: "public/1584038190074-audioplayer.jpg"), Test.PostById(id: "9201b562-d5d9-4e6f-8b21-4c3f691b62ee", album_id: "57e19ce4-ef2e-499e-aa76-b3e32ff80b4e", name: Optional("Happy Sixth "), path: "public/1584038190075-Happy_Sixth.mp3"), Test.PostById(id: "f3ca3cb8-7670-4e0b-87b3-80f8a2d3d96f", album_id: "57e19ce4-ef2e-499e-aa76-b3e32ff80b4e", name: Optional("How it Began "), path: "public/1584038190273-How_it_Began.mp3"), Test.PostById(id: "63c58525-3115-4882-b280-66c675c64ded", album_id: "57e19ce4-ef2e-499e-aa76-b3e32ff80b4e", name: Optional("It Doesnt Need To Be More Than That "), path: "public/1584038190375-It_Doesnt_Need_To_Be_More_Than_That.mp3"), Test.PostById(id: "af5c2f62-fa97-4736-a003-cd876324d28c", album_id: "57e19ce4-ef2e-499e-aa76-b3e32ff80b4e", name: Optional("Kindergarden "), path: "public/1584038190484-Kindergarden.mp3")]

我要像这样在其他视图中传递它:

struct ContentView: View {
  @ObservedObject var model = PostListViewModel()
  @ObservedObject var model2 = PostListViewByIdModel()

        var body: some View {

          NavigationView {
                           List(model.posts) { post in
                            VStack{
                                   Text("Title: ").bold()
                                       + Text("\(post.title)")
                                NavigationLink(destination: Album(post: post, post2: self.model2.postsById)) {

在线NavigationLink(destination: Album(post: post, post2: self.model2.postsById))

但是由于Album(post: post, post2: self.model2.postsById))在列表中,因此它并不会只传递所有数据。

但是我正在尝试使用所有这样的数据:

import SwiftUI

struct Album: View {
    var post:Post
    var post2:[PostById]

     var body: some View {
             VStack {
               Text("Title: ").bold()
                   + Text("\(post.title)")
                ImageView(withURL: "http://localhost:8000/\(post.path.replacingOccurrences(of: " ", with: "%20"))")
                   Text("Description: ").bold()
                       + Text("\(post.description)")
                List(self.post2.filter { i in i.album_id == post.id }) { post in
                   Text("\(post.name ?? "title")")
                }
                }

            }

           }

在我的列表中进行过滤。

那么如何将model2.postsById作为完整数据传递到我的列表中或列表之外,然后传递到struct Album中,以便可以在我的列表中使用

0 个答案:

没有答案