【发布时间】:2019-11-13 23:11:29
【问题描述】:
我正在尝试使用为 SwiftUI 中的 UIKit 创建的第三方库,例如 BetterSegmentedControl 库 (https://github.com/gmarm/BetterSegmentedControl)
需要一个 Selector 女巫采用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'
【问题讨论】:
-
查看我的评论。工作得很好