Class: MotionBindable::Strategies::UITextField

Inherits:
MotionBindable::Strategy show all
Includes:
MotionBindable::StrategyHelpers
Defined in:
lib/strategies/ui_text_field.rb

Instance Attribute Summary

Attributes inherited from MotionBindable::Strategy

#attr_name, #bound, #object

Instance Method Summary collapse

Methods included from MotionBindable::StrategyHelpers

#observeValueForKeyPath, #observe_object, #stop_observe_object

Methods inherited from MotionBindable::Strategy

#bind, #bound_value, find_by_reference, #initialize, #object_value, register_strategy

Constructor Details

This class inherits a constructor from MotionBindable::Strategy

Instance Method Details

#on_bound_change(new = nil) ⇒ Object



18
19
20
# File 'lib/strategies/ui_text_field.rb', line 18

def on_bound_change(new = nil)
  self.attribute = (new || bound.text)
end

#on_object_change(new = nil) ⇒ Object



22
23
24
# File 'lib/strategies/ui_text_field.rb', line 22

def on_object_change(new = nil)
  @bound.text = (new || attribute)
end

#start_observing_boundObject



5
6
7
8
9
10
11
12
# File 'lib/strategies/ui_text_field.rb', line 5

def start_observing_bound
  @bound_observer = NSNotificationCenter.defaultCenter.addObserverForName(
    UITextFieldTextDidChangeNotification,
    object: bound,
    queue: nil,
    usingBlock: proc { |_| on_bound_change }
  )
end

#start_observing_objectObject



14
15
16
# File 'lib/strategies/ui_text_field.rb', line 14

def start_observing_object
  observe_object { |_, new| on_object_change(new) }
end

#unbindObject



26
27
28
29
30
# File 'lib/strategies/ui_text_field.rb', line 26

def unbind
  NSNotificationCenter.defaultCenter.removeObserver(@bound_observer)
  stop_observe_object
  super
end