Skip to content

Instantly share code, notes, and snippets.

@auguststurm
Last active May 18, 2017 01:28
Show Gist options
  • Save auguststurm/dd98f1ea28d8892595052455efbfe46e to your computer and use it in GitHub Desktop.
Save auguststurm/dd98f1ea28d8892595052455efbfe46e to your computer and use it in GitHub Desktop.
Swift 3.0 & Signals
// 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)
}
}
// 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)
}
}
// 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