【问题标题】:Send data to next view - SwiftUI将数据发送到下一个视图 - SwiftUI
【发布时间】:2020-08-19 06:18:55
【问题描述】:

所以我一直在尝试使用 SwiftUI 获得简单的数据传递。

基本上,下面的脚本会打印出一个项目列表(在 HStack 中),然后我将每个项目链接到我们的 Podcast() 视图。

我要做的是将播客名称传递到下一个视图。我如何实现 这?因为所有的例子都是关于 int 的,我没有使用我使用的字符串。

import SwiftUI
import RemoteImage

struct ContentView: View {

    @State private var showAlert = false
    @State var posts: [Program] = []

   var body: some View {
        NavigationView {
            if posts.isEmpty {
                Text("Loading")
            } else {
                ScrollView(.horizontal, showsIndicators: false) {
                    HStack(alignment: .bottom, spacing: 10) {
                        ForEach(posts) { post in
                            //return
                            NavigationLink(destination: Podcasts()){
                            RemoteImage(type: .url(URL(string:post.icon)!), errorView: { error in
                                Text(error.localizedDescription)
                            }, imageView: { image in
                                image
                                .resizable()
                                .renderingMode(.original)
/*                                .clipShape(Circle())
                                .shadow(radius: 10)
                                .overlay(Circle().stroke(Color.red, lineWidth: 5))*/
                                .aspectRatio(contentMode: .fit)
                                .frame(width:200, height:200)
                            }, loadingView: {
                                Text("Loading ...")
                            })
                            }
                        }
                    }.frame(height: 200)
                }.frame(height: 200)
            }
        }.onAppear {
            Api().getPosts { (posts) in
                self.posts = posts
            }
        }.navigationBarTitle(Text("Home"))
    }
}



struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

播客视图

import SwiftUI

struct Podcasts: View {
    
 
    var body: some View {
      
        NavigationView{
            Text("Hello")
        }.navigationBarTitle(Text("Podcast"))
    
    }
        
}


struct Podcasts_Previews: PreviewProvider {
    static var previews: some View {
        Podcasts()
    }
}

【问题讨论】:

    标签: view swiftui


    【解决方案1】:

    将 post 作为构造函数参数传递,例如

    ForEach(posts) { post in
        //return
        NavigationLink(destination: Podcasts(post: post)){
    

    现在

    struct Podcasts: View {
        let post: Program
     
        var body: some View {
    
            // !! DON'T ADD SECOND NAVIGATION VIEW IN STACK 
            // !! - THERE MUST BE ONLY ONE
    
            Text(post.name)
               .navigationBarTitle(Text("Podcast"))
        }
    }
    

    【讨论】:

    • 但这给了我一个错误参数传递给在 navigationLink 上不接受任何参数的调用 - 我确实尝试过,但一直收到错误。
    • 使用 Xcode 11.4 / iOS 13.4 测试。可能是你在适应你的代码时遗漏了什么,或者没有提供一些代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多