Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

I'm expanding the functionality of this SO answerthis SO answer.

I'm expanding the functionality of this SO answer.

I'm expanding the functionality of this SO answer.

Rollback to Revision 5
Source Link
rolfl
  • 97.5k
  • 17
  • 218
  • 418
let btn = UIButton()
btn.on(.TouchUpInside) { sender, _ in
    sender.setTitle(titleLabel.text = "Pushed !!", forState: .Normal)
}

Any ideas?


Added: to reply the @nhgrif answer:

Why I did not implement the sender only version like this:

func on<T: UIControl>(events:UIControlEvents, _ callback: T -> Void) -> EventHandlerId {
    assert(self.isKindOfClass(T), "The handler must receive \(NSStringFromClass(self.dynamicType)) or UIControl")
    return self._on(events, EventHandler({ sender, _ in callback(sender as T) }, oneshot: true))
}

That is because of a purely technical problem: It conflict with () -> Void version.

btn.on(.TouchUpInside) { return }                       // OK: () -> Void
btn.on(.TouchUpInside) { (sender: UIButton) in return } // OK: T -> Void
btn.on(.TouchUpInside) { sender in return }             // NG: () -> Void

In the last case, the compiler infers the type of sender as () :(

And why I declared EventHandler as private, that is I don't want users do like this:

let handler = UIControl.EventHandler({ ... }, oneshot: true)

btn.on(.TouchUpInside, handler)
btn.on(.TouchDown, handler)
btn.off(handler)

With my current code, .off() removes the handler from .AllEvents, and from associated objects. There is no way to remove it from specific event. Maybe, if we do want to do that, we can remember the registered events and handle them, but It's too much I think. Moreover, for such use case, we can simply use addTarget() and removeTarget() with custom handler object.

let btn = UIButton()
btn.on(.TouchUpInside) { sender, _ in
    sender.setTitle("Pushed !!", forState: .Normal)
}

Any ideas?


Added: to reply the @nhgrif answer:

Why I did not implement the sender only version like this:

func on<T: UIControl>(events:UIControlEvents, _ callback: T -> Void) -> EventHandlerId {
    assert(self.isKindOfClass(T), "The handler must receive \(NSStringFromClass(self.dynamicType)) or UIControl")
    return self._on(events, EventHandler({ sender, _ in callback(sender as T) }, oneshot: true))
}

That is because of a purely technical problem: It conflict with () -> Void version.

btn.on(.TouchUpInside) { return }                       // OK: () -> Void
btn.on(.TouchUpInside) { (sender: UIButton) in return } // OK: T -> Void
btn.on(.TouchUpInside) { sender in return }             // NG: () -> Void

In the last case, the compiler infers the type of sender as () :(

And why I declared EventHandler as private, that is I don't want users do like this:

let handler = UIControl.EventHandler({ ... }, oneshot: true)

btn.on(.TouchUpInside, handler)
btn.on(.TouchDown, handler)
btn.off(handler)

With my current code, .off() removes the handler from .AllEvents, and from associated objects. There is no way to remove it from specific event. Maybe, if we do want to do that, we can remember the registered events and handle them, but It's too much I think. Moreover, for such use case, we can simply use addTarget() and removeTarget() with custom handler object.

let btn = UIButton()
btn.on(.TouchUpInside) { sender, _ in
    sender.titleLabel.text = "Pushed !!"
}

Any ideas?

added 11 characters in body
Source Link
rintaro
  • 151
  • 6
let btn = UIButton()
btn.on(.TouchUpInside) { sender, _ in
    sender.titleLabel.text = setTitle("Pushed !!", forState: .Normal)
}
btn.on(.TouchUpInside) { return }                       // OK: () -> Void
btn.on(.TouchUpInside) { (sender: UIButton) in return } // OK: T -> Void
btn.on(.TouchUpInside) { sender in return }             // NG: () -> Void
let btn = UIButton()
btn.on(.TouchUpInside) { sender, _ in
    sender.titleLabel.text = "Pushed !!"
}
btn.on(.TouchUpInside) { return }                    // OK: () -> Void
btn.on(.TouchUpInside) { sender:UIButton in return } // OK: T -> Void
btn.on(.TouchUpInside) { sender in return }          // NG: () -> Void
let btn = UIButton()
btn.on(.TouchUpInside) { sender, _ in
    sender.setTitle("Pushed !!", forState: .Normal)
}
btn.on(.TouchUpInside) { return }                       // OK: () -> Void
btn.on(.TouchUpInside) { (sender: UIButton) in return } // OK: T -> Void
btn.on(.TouchUpInside) { sender in return }             // NG: () -> Void
added reply.
Source Link
rintaro
  • 151
  • 6
Loading
edited tags
Link
nhgrif
  • 25.3k
  • 3
  • 63
  • 129
Loading
Corrected the assertion error message.
Source Link
rintaro
  • 151
  • 6
Loading
added 6 characters in body
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237
Loading
code cleanup
Source Link
rintaro
  • 151
  • 6
Loading
Source Link
rintaro
  • 151
  • 6
Loading