简体   繁体   English

如何将通用 SwiftUI 视图添加到另一个视图?

[英]How to add a generic SwiftUI view to another view?

When trying to use a generic type to init/add a view to another SwiftUI view, I get the error Type 'T' has no member 'init' .当尝试使用泛型类型初始化/添加视图到另一个 SwiftUI 视图时,我收到错误Type 'T' has no member 'init' How can this be solved?如何解决?

I guess the problem is, that View is just a protocol which only requires a body property and nothing more.我想问题是, View只是一个协议,它只需要一个body属性,仅此而已。 But what type could be used instead?但是可以改用什么类型呢?

Simple Example:简单示例:

Trying to use TrippleView<T: View> which should create/show three views of type T .尝试使用应该创建/显示T类型的三个视图的TrippleView<T: View> Adding views ( RedRectView or BlueCircleView ) is no problem.添加视图( RedRectViewBlueCircleView )没问题。 Passing these views as generic parameter fails:将这些视图作为通用参数传递失败:

struct SomeView: View {
    var body: some View {
        TrippleView<RedRectView>()
        TrippleView<BlueCircleView>()
    }
}

struct TrippleView<T: View>: View {
    var body: some View {
        VStack {
            // Does NOT work: Type 'T' has no member 'init'
            T()
            T()
            T()
            
            // Works fine
            // RedRectView()
            // BlueCircleView()
            // RedRectView()
        }
    }
}

struct RedRectView: View {...}
struct BlueCircleView: View {...}

EDIT:编辑:

The TrippleView is of course just an example. TrippleView当然只是一个例子。 I would like to use the generic view just as any other generic type: To a common base "type" in different scenarios.我想像使用任何其他泛型类型一样使用泛型视图:在不同场景中使用通用的基本“类型”。

For example two versions of a list view which use a generic CellView to display the same data in two different styles / layouts.例如,两个版本的列表视图使用通用CellView在两个不同的 styles / 布局中显示相同的数据。

You need a ViewBuilder inside your struct to initialize the view.您需要在结构中使用 ViewBuilder 来初始化视图。 Here's an example,这是一个例子,

Struct that takes in generic view and returns a view.接受通用视图并返回视图的结构。

struct TripleView<T: View>: View {
// simple example that takes in one parameter.
var someView: T

init(@ViewBuilder someView: () -> T) {
    self.someView = someView()
}

var body: some View {
    // You can modify your viewes here. 
    someView
        .foregroundColor(.red)
    someView
        .foregroundColor(.blue)
    someView
        .foregroundColor(.green)
    }
}

Or you can take in multiple parameters或者你可以接受多个参数

struct TripleViewsWithParam<T1: View, T2: View, T3: View>: View {
// you can take as many parameters as you want.
var someView1: T1
var someView2: T2
var someView3: T3

init(@ViewBuilder someView1: () -> T1, @ViewBuilder someView2: () -> T2, @ViewBuilder someView3: () -> T3) {
    self.someView1 = someView1()
    self.someView2 = someView2()
    self.someView3 = someView3()
}

var body: some View {
    VStack {
        someView1
        someView2
        someView3
    }
}
}

Calling the struct in your main view在主视图中调用结构

struct SomeView: View {
var body: some View {
    VStack{
        TripleView {
            Text("Triple View Example")
        }
        TripleViewsWithParam(
            someView1: {
                Text("View 1")
            }, someView2: {
                Text("View 2")
            }, someView3: {
                Text("View 3")
            }
        )
    }

}
}

结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM