如何访问 SwiftUI 中的子视图?

2024-01-13

我正在开发 SwiftUI,感觉它与 React 非常相似。刚才我正在自定义一个SwiftUI的Button,遇到一个问题,无法动态访问Button的子视图 以下代码是我要做的:

struct FullButton : View {
  var action: () -> Void
  var body: some View {
    Button(action: action) {
      // render children views here even what is that
      children
    }
  }
}

和用法:

VStack {
  FullButton(action: {
    print('touched')
  }) {
    Text("Button")
  }
}

请问我是不是有什么错误的想法?


Update

取决于@graycampbell的回答,我尝试如下

struct FullButton<Label> where Label : View {
    var action: () -> Void
    var label: () -> Label

    init(action: @escaping () -> Void, @ViewBuilder label: @escaping () -> Label) {
        self.action = action
        self.label = label
    }

    var body: some View {
        Button(action: action, label: label)
    }
}

So the FullButton看起来和它本身一样好。但我此时在使用中又出现了一个编译错误。

VStack {
    FullButton(action: { print("touched") }) {
        Text("Fullbutton")
    }
}

错误是Referencing initializer 'init(alignment:spacing:content:)' on 'VStack' requires that 'FullButton<Text>' conform to 'View'.
它的意思是FullButton还没有返回body now?
我不知道为什么是因为FullButton仍在延伸View class.
请让我知道什么是正确的body该类类型的定义。


如果我正确理解你的问题,这就是你正在寻找的:

struct FullButton<Label>: View where Label: View {
    var action: () -> Void
    var label: () -> Label

    var body: some View {
        Button(action: self.action, label: self.label)
    }
}

这将允许您传递想要在按钮上显示的任何内容,这意味着您此处的代码现在可以工作:

FullButton(action: {
    print("touched")
}) {
    Text("Button")
}

Update

多次查看您的问题后,我意识到您的困惑源于对创建正常情况时发生的事情的误解Button.

在下面的代码中,我正在创建一个Button。该按钮有两个参数 -action and label.

Button(action: {}, label: {
    Text("Button")
})

如果我们查看文档Button,我们看到它是这样声明的:

struct Button<Label> where Label : View

如果我们查看初始化器,我们会看到:

init(action: @escaping () -> Void, @ViewBuilder label: () -> Label)

Both action and label预计关闭。action期望一个返回类型为Void, and label期望一个@ViewBuilder返回类型为的闭包Label。正如声明中所定义的Button, Label是一个泛型,代表一个View,所以真的,label期待一个返回 a 的闭包View.

这并不是独一无二的Button. Take HStack, 例如:

struct HStack<Content> where Content : View

init(alignment: VerticalAlignment = .center, spacing: Length? = nil, @ViewBuilder content: () -> Content)

Content在这里有同样的目的Label确实在Button.

还有其他需要注意的事情 - 当我们创建这样的按钮时......

Button(action: {}) {
    Text("Button")
}

...我们实际上正在做同样的事情:

Button(action: {}, label: {
    Text("Button")
})

在 Swift 中,当方法调用中的最后一个参数是闭包时,我们可以省略参数标签并将闭包附加到右括号的外部。

在 SwiftUI 中,您不能将内容隐式传递给任何View. The View必须明确接受@ViewBuilder其初始化程序中的闭包。

所以,你不能通过@ViewBuilder关闭到FullButton unless FullButton接受一个@ViewBuilder闭包作为其初始值设定项中的参数,如我的答案开头所示。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何访问 SwiftUI 中的子视图? 的相关文章

随机推荐