I searched a lot and haven't found anything that really helped to solve my problem...
I'm trying to build a simple UIControl from scratch, like a UIButton. It can't be subclass of UIControl for particular reasons.
I need this control to do something like this:
myControl.addTarget(target: AnyObject?, action: Selector, forControlEvents: UIControlEvents)
The problem is that the implementation of the method that execute this selector when the button is touched needs the "performSelector:" method.
Swift doesn't have a "performSelector:". So I taught it could be implemented using closures.
I couldn't figure out how to capture the objects I want to modify inside the closure. And I'm not sure how I would deal with reference cycles and other things like that.
I don't even know if I am on the right path to succeed this. I'm sure you guys can put me on the right track!
I'm from Brazil, sorry for my poor English! Thanks! :D
Here is what a 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?"
})
}
}