Last active
May 18, 2017 01:28
-
-
Save auguststurm/dd98f1ea28d8892595052455efbfe46e to your computer and use it in GitHub Desktop.
Swift 3.0 & Signals
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
// The singleton type object | |
import Foundation | |
import Signals // -> https://github.com/artman/Signals | |
class SignalStation | |
{ | |
static let shared = SignalStation() | |
let onValueUpdate = Signal<Int>() | |
func updateValue(newValue : Int) { | |
self.onValueUpdate.fire(newValue) | |
} | |
} |
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
// subview anywhere in your app | |
import UIKit | |
class View : UIView { | |
let signalStation = SignalStation.shared | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
let button : UIButton = UIButton(type: UIButtonType.roundedRect) | |
// ... | |
button.addTarget(self, action: #selector(updateAction(button:)), for: .touchUpInside) | |
} | |
required init?(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
} | |
func updateAction(button: UIButton) { | |
signalStation.updateValue(newValue: 42) | |
} | |
} |
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
// class you want to handle as the value is changed | |
import UIKit | |
import Signals // -> https://github.com/artman/Signals | |
class ViewController : UIViewController { | |
let signalStation = SignalStation.shared | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
signalStation.onValueUpdate.subscribe(on: self) { (value) in | |
self.handleNewValue(value) | |
} | |
} | |
func handleNewValue(value : Int) { | |
print("New Value: \(value)") // -> 42 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment