I'm trying to use 3rd party libraries that are created for UIKit in SwiftUI, for example, the BetterSegmentedControl
library (https://github.com/gmarm/BetterSegmentedControl)
requires a Selector witch takes an objc
function to handle users input.
is there even a way of handling this in 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)
}
}
for this code there are 2 errors:
@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'