Last active
April 4, 2017 23:34
-
-
Save nicolastinkl/e33ab89b56b27af2808b to your computer and use it in GitHub Desktop.
how to use 'Currying' and implement UIControl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol TargetAction{ | |
func performAction() | |
} | |
struct TargetActionWrapper<T:AnyObject>:TargetAction { | |
weak var target : T? | |
var action:(T)->()->() | |
func performAction() { | |
if let t = target{ | |
action(t)() // real return a function ()->() | |
} | |
} | |
} | |
enum ControlsEvents{ | |
case TouchUpInside | |
case ValueChanged | |
case TouchDownOutside | |
} | |
class TKControl { | |
var actions = [ControlsEvents:TargetAction]() | |
//settargetaction Target/Action/ControlStatus | |
func setTarget<T : AnyObject>(target:T,action:(T)->()->(),controlEvent:ControlsEvents) | |
{ | |
let item = TargetActionWrapper(target: target, action: action) | |
actions[controlEvent] = item | |
} | |
//add events | |
func performActionForControlEvent(controlEvent: ControlsEvents) { | |
actions[controlEvent]?.performAction() | |
} | |
//remove events | |
func removeTargetForControlEvent(controlEvent: ControlsEvents) { | |
actions[controlEvent] = nil | |
} | |
func click() | |
{ | |
} | |
func log() | |
{ | |
// println("some body jiekuan summary") | |
} | |
} |
Author
nicolastinkl
commented
Nov 12, 2014
is setting button.setTarget(self, action: ViewController.onButtonTap, controlEvent: ControlsEvents.TouchUpInside) enough? Who is going to call func performActionForControlEvent(controlEvent: ControlsEvents)?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment