I searched a lot and did not find anything that really helped solve my problem ...
I am trying to create a simple UIControl from scratch, like UIButton. It cannot be a subclass of UIControl for certain reasons.
I need this control to do something like this:
myControl.addTarget(target: AnyObject?, action: Selector, forControlEvents: UIControlEvents)
The problem is that to implement the method that executes this selector when the button is touched, the "make a choice:" method is needed.
Swift does not have a "performSelector:". Therefore, I taught him how to implement it using closures.
I could not figure out how to capture the objects that I want to change inside the closure. And I'm not sure how I will do reference cycles and other things like this.
I don’t even know if I am on the right track. I'm sure you guys can put me on the right track!
I am from Brazil, I apologize for my poor Englishman! Thank you !: D
Here is what you have so far ...
struct ClosureForEvent {
var closure:(control:MyControl!)->()
var event:UIControlEvents
}
class MyControl {
private var closures:[ClosureForEvent]?
init() {}
func addClosureFor(event:UIControlEvents, closure:(control:MyControl!)->()) {
if closures == nil {
closures = [ClosureForEvent(closure: closure, event: event)]
}
else {
closures!.append(ClosureForEvent(closure: closure, event: event))
}
}
func executeClosuresOf(event:UIControlEvents) {
if closures != nil {
for closure in closures! {
if closure.event == event {
closure.closure(control: control)
}
}
}
}
}
class Test {
var testProperty = "Default String"
init() {
let control = MyControl()
control.addClosureFor(UIControlEvents.TouchUpInside, closure: { (control) -> () in
self.testProperty = "This is making a reference cycle?"
})
}
}
source
share