Class: NSNotificationCenter
- Inherits:
-
Object
- Object
- NSNotificationCenter
- Defined in:
- lib/bean/nsnotificationcenter_additions.rb
Overview
Extensions to the NSNotificationCenter class
Class Method Summary collapse
-
.observe(name, &blk) ⇒ nil
Setup an observer for the given notification and execute block when seen.
-
.post(options) ⇒ nil
Post a notification to the default center.
Class Method Details
.observe(name, &blk) ⇒ nil
Setup an observer for the given notification and execute block when seen. Then block will be passed the notification object.
35 36 37 38 39 40 41 42 |
# File 'lib/bean/nsnotificationcenter_additions.rb', line 35 def self.observe(name, &blk) @observers ||= [] @observers << Class.new do define_method("call_#{name}") { |notice| blk.call(notice) } end.new NSNotificationCenter.defaultCenter.addObserver(@observers.last, selector:"call_#{name}:", name:name, object:nil) end |
.post(options) ⇒ nil
Post a notification to the default center.
NSNotificationCenter.post provides a convenience wrapper around posting messages to the defaultCenter.
17 18 19 20 21 |
# File 'lib/bean/nsnotificationcenter_additions.rb', line 17 def self.post() NSNotificationCenter.defaultCenter.postNotificationName([:name], object:[:object], userInfo:[:info]) end |