我正在尝试使用在SwiftUI中为UIKit创建的第三方库,例如BetterSegmentedControl
库(https://github.com/gmarm/BetterSegmentedControl)
需要选择器女巫使用objc
函数来处理用户输入。
在SwiftUI中甚至没有办法解决这个问题?
struct ContentView : UIViewRepresentable {
func makeUIView(context: Context) -> BetterSegmentedControl {
BetterSegmentedControl()
}
func updateUIView(_ view: BetterSegmentedControl, context: Context) {
let control = BetterSegmentedControl(
frame: CGRect(x: 0, y: 0, width: 300, height: 44),
segments: LabelSegment.segments(withTitles: ["One", "Two", "Three"],
normalTextColor: .lightGray,
selectedTextColor: .white),
index: 1,
options: [.backgroundColor(.darkGray),
.indicatorViewBackgroundColor(.blue)])
view.addSubview(control)
@objc func controlValueChanged(_ sender: BetterSegmentedControl) {
}
control.addTarget(self, action: #selector(controlValueChanged(_:)), for: .valueChanged)
view.addSubview(control)
}
}
对于此代码,有2个错误:
@objc can only be used with members of classes, @objc protocols, and concrete extensions of classes
Argument of '#selector' cannot refer to local function 'controlValueChanged'
最佳答案
我最终添加了一个帮助器类:
struct MainView: View {
let helper = MainViewHelper()
// somewhere, sometimes...
vc.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .cancel, target: self.helper, action: #selector(self.helper.closeAction))
}
class MainViewHelper {
@objc func closeAction() {
// whatever
}
}
关于ios - SwiftUI中的选择器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56867114/