将操作添加到使用UiviewRementable创建的按钮
我正在使用a
/a>,在使用Uikit的背后fluentui#fulentui#button#button
,我需要在SwiftUi视图中显示该按钮,并且我正在尝试切换@State
属性或添加#selector
该按钮,但是我无法做到这一点,
我创建了一个通用uiviewRementable
结构,以帮助我在SwiftUi视图中嵌入任何Uiview,之后 tutorial :
struct Anything<Wrapper : UIView>: UIViewRepresentable {
typealias Updater = (Wrapper, Context) -> Void
var makeView: () -> Wrapper
var update: (Wrapper, Context) -> Void
init(_ makeView: @escaping @autoclosure () -> Wrapper,
updater update: @escaping (Wrapper) -> Void) {
self.makeView = makeView
self.update = { view, _ in update(view) }
}
func makeUIView(context: Context) -> Wrapper {
makeView()
}
func updateUIView(_ view: Wrapper, context: Context) {
update(view, context)
}
}
我有以下代码:
import SwiftUI
import FluentUI
struct MyView: View {
@State var isGreen = true
var body: some View {
VStack {
Text("Hello, World!")
.background(isGreen ? Color.green : Color.blue)
Spacer().frame(height: 20)
Anything(FluentUI.Button(style: .primaryFilled)) {
$0.setTitle("Try me!", for: .normal)
}
.frame(height: 30)
.padding()
}
}
}
struct Anything<Wrapper: UIView>: UIViewRepresentable {
typealias Updater = (Wrapper, Context) -> Void
var makeView: () -> Wrapper
var update: (Wrapper, Context) -> Void
var action: (() -> Void)?
init(_ makeView: @escaping @autoclosure () -> Wrapper,
updater update: @escaping (Wrapper) -> Void) {
self.makeView = makeView
self.update = { view, _ in update(view) }
}
func makeUIView(context: Context) -> Wrapper {
makeView()
}
func updateUIView(_ view: Wrapper, context: Context) {
update(view, context)
}
}
struct SwiftUIView_Previews: PreviewProvider {
static var previews: some View {
MyView()
}
}
如果我尝试添加以下添加:我
$0.addTarget(self, action: #selector(toggleColor), for: .touchUpInside)
得到:
func toggleColor() {
isGreen = !isGreen
}
我得到此错误:我得到此错误:
Argument of '#selector' refers to instance method 'toggleColor()' that is not exposed to Objective-C
而且,如果我将@OBJC
添加到该方法中,我会收到此错误:
@objc can only be used with members of classes, @objc protocols, and concrete extensions of classes
并且由于我的 struct不是
button
swiftui,我无法将操作
参数添加为通常
如何以这种方式添加目标/操作?
I am using a FluentUI#button
, which behind uses UIKit
I need to display that button in a SwiftUI View, and I'm trying to toggle an @State
property or add a #selector
to the button, but I'm not able to do it
I created a generic UIViewRepresentable
structure to help me embed any UIView in my SwiftUI Views, following this tutorial:
struct Anything<Wrapper : UIView>: UIViewRepresentable {
typealias Updater = (Wrapper, Context) -> Void
var makeView: () -> Wrapper
var update: (Wrapper, Context) -> Void
init(_ makeView: @escaping @autoclosure () -> Wrapper,
updater update: @escaping (Wrapper) -> Void) {
self.makeView = makeView
self.update = { view, _ in update(view) }
}
func makeUIView(context: Context) -> Wrapper {
makeView()
}
func updateUIView(_ view: Wrapper, context: Context) {
update(view, context)
}
}
And I have the following code:
import SwiftUI
import FluentUI
struct MyView: View {
@State var isGreen = true
var body: some View {
VStack {
Text("Hello, World!")
.background(isGreen ? Color.green : Color.blue)
Spacer().frame(height: 20)
Anything(FluentUI.Button(style: .primaryFilled)) {
$0.setTitle("Try me!", for: .normal)
}
.frame(height: 30)
.padding()
}
}
}
struct Anything<Wrapper: UIView>: UIViewRepresentable {
typealias Updater = (Wrapper, Context) -> Void
var makeView: () -> Wrapper
var update: (Wrapper, Context) -> Void
var action: (() -> Void)?
init(_ makeView: @escaping @autoclosure () -> Wrapper,
updater update: @escaping (Wrapper) -> Void) {
self.makeView = makeView
self.update = { view, _ in update(view) }
}
func makeUIView(context: Context) -> Wrapper {
makeView()
}
func updateUIView(_ view: Wrapper, context: Context) {
update(view, context)
}
}
struct SwiftUIView_Previews: PreviewProvider {
static var previews: some View {
MyView()
}
}
And if I try to add this:
$0.addTarget(self, action: #selector(toggleColor), for: .touchUpInside)
With:
func toggleColor() {
isGreen = !isGreen
}
I get this error:
Argument of '#selector' refers to instance method 'toggleColor()' that is not exposed to Objective-C
And if I add @objc
to the method I get this error:
@objc can only be used with members of classes, @objc protocols, and concrete extensions of classes
And as my Anything
struct isn't a Button
from SwiftUI, I cannot add the action
parameter as normally
How can I add a target/action to my button in this way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是可能的解决方案的演示 - 我们需要Uikit Objective -C选择器和SwiftUI Swift功能之间的包装器。
用Xcode 13.3/ios 15.4
这是主要部分(用于简单的uibutton而不是fluentui.button):
完整的测试模块在这里
Here is a demo of possible solution - we need a wrapper between UIKit objective-c selectors and SwiftUI swift function.
Tested with Xcode 13.3 / iOS 15.4
Here is main part (used UIButton instead of FluentUI.Button for simplicity):
Complete test module is here