1

这是一个简单的示例,但我无法弄清楚如何将 test 的值传递到传入的目标视图中。网络上的所有示例在传入时都会形成“完全”视图。

(我正在考虑使用一个函数,我将值传递回父级并让它返回形成的视图,但那是向后的。)

这是为了支持一个 swift 包,其中一个视图由使用该包的应用程序提供。(然后,我提出的解决方案将让应用程序支持 GetView 函数。我希望应用程序只传递一个采用已定义参数的视图。

import SwiftUI

struct DestinationLinkView: View {
    let title: String
    var body: some View {
        Text(title)
    }
}


struct ContentView2<LinkView: View>: View {
    let linkView: LinkView
    let testList = ["Test1", "Test2", "Test3"]
    var body: some View {
        HStack {
            NavigationView {
                List {
                    ForEach(testList, id: \.self) { test in
                                                  /*====*/
                        /* *******
                        I want to pass test into self.linkView
                        but cannot figure out how.
                        */
                    NavigationLink(destination: self.linkView) {Text(test)}
// Something like
//                  NavigationLink(destination: self.linkView(title: test)) {Text(test)}
// but that does not work.
                }
            }
        }
    }
}

init(@ViewBuilder linkView: ()->LinkView) {
        self.linkView = linkView()
    }
}

struct ContentView1: View { 
    var body: some View {
        ContentView2() {
            // I want title to be set in ContentView2 when it
            // calls the desitnation
            DestinationLinkView(title: "LINK")
        }
   }
}       
struct ContentView: View {
    var body: some View {
        ContentView1() // calling here so that I don't have to change @main in another file
    }
}

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

1 回答 1

2

您可以将参数作为视图构建器参数传递。

这是可能的方法(使用 Xcode 12.1 / iOS 14.1 测试)

struct ContentView2<LinkView: View>: View {
    let linkView: (String)->LinkView
    let testList = ["Test1", "Test2", "Test3"]
    var body: some View {
        HStack {
            NavigationView {
                List {
                    ForEach(testList, id: \.self) { test in
                        NavigationLink(destination: self.linkView(test)) {Text(test)}
                    }
                }
            }
        }
    }
    
    init(@ViewBuilder linkView: @escaping (String)->LinkView) {
        self.linkView = linkView
    }
}

struct ContentView1: View {
    var body: some View {
        ContentView2() { value in
            DestinationLinkView(title: value)
        }
   }
}
于 2020-12-27T15:21:45.053 回答