【发布时间】:2019-06-13 15:38:05
【问题描述】:
我正在尝试找到一种方法来触发一个操作,当在swiftUI 中点击一个按钮时,该操作将调用我的UIView 中的一个函数。
这是我的设置:
foo()(UIView) 需要在Button(SwiftUI) 被点击时运行
我使用 AVFoundation 框架的自定义 UIView 类
class SomeView: UIView {
func foo() {}
}
要在 swiftUI 中使用我的 UIView,我必须将它包装在 UIViewRepresentable
struct SomeViewRepresentable: UIViewRepresentable {
func makeUIView(context: Context) -> CaptureView {
SomeView()
}
func updateUIView(_ uiView: CaptureView, context: Context) {
}
}
托管我的 UIView() 的 SwiftUI 视图
struct ContentView : View {
var body: some View {
VStack(alignment: .center, spacing: 24) {
SomeViewRepresentable()
.background(Color.gray)
HStack {
Button(action: {
print("SwiftUI: Button tapped")
// Call func in SomeView()
}) {
Text("Tap Here")
}
}
}
}
}
【问题讨论】:
-
您可以在这里查看我对类似问题的回答:stackoverflow.com/a/70559743/5379093