Class: NSNotificationCenter

Inherits:
Object
  • Object
show all
Defined in:
lib/bean/nsnotificationcenter_additions.rb

Overview

Extensions to the NSNotificationCenter class

Class Method Summary collapse

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.

Examples:

NSNotificationCenter.observe(:my_message) do |notice|
  puts "My Message Received: #{notice.userInfo.inspect}"
end

Parameters:

  • name (String|Symbol)

    The notification name to listen for

  • blk (Proc)

    The block to execute when notification is +received

Returns:

  • (nil)


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.

Examples:

NSNotificationCenter.post(:name => :my_message, :info => {:hi => 1})

Parameters:

  • options (Hash)

    Options for the notification

Options Hash (options):

  • :name (String|Symbol)

    The notification name to send

  • :object (String)

    The object to send

  • :info (String)

    The userInfo to send

Returns:

  • (nil)


17
18
19
20
21
# File 'lib/bean/nsnotificationcenter_additions.rb', line 17

def self.post(options)
  NSNotificationCenter.defaultCenter.postNotificationName(options[:name],
                                                   object:options[:object],
                                                 userInfo:options[:info])
end