Class: MotionBindable::Strategies::UITextField

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

Constant Summary

Constants inherited from MotionBindable::Strategy

MotionBindable::Strategy::WATCH_TICK

Instance Attribute Summary

Attributes inherited from MotionBindable::Strategy

#attr_name, #bound, #object

Instance Method Summary collapse

Methods inherited from MotionBindable::Strategy

#bind, find_by_reference, #initialize, register_strategy

Constructor Details

This class inherits a constructor from MotionBindable::Strategy

Instance Method Details

#observeValueForKeyPath(_, ofObject: _, change: _, context: _) ⇒ Object

NSKeyValueObserving Protocol



40
41
42
# File 'lib/strategies/ui_text_field.rb', line 40

def observeValueForKeyPath(_, ofObject: _, change: _, context: _)
  on_object_change
end

#on_bound_change(new = nil) ⇒ Object



24
25
26
# File 'lib/strategies/ui_text_field.rb', line 24

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

#on_object_change(new = nil) ⇒ Object



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

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
17
18
19
20
21
22
# File 'lib/strategies/ui_text_field.rb', line 14

def start_observing_object
  # Observe the attribute
  object.addObserver(
    self,
    forKeyPath: @attr_name,
    options: NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld,
    context: nil
  )
end

#unbindObject



32
33
34
35
36
# File 'lib/strategies/ui_text_field.rb', line 32

def unbind
  NSNotificationCenter.defaultCenter.removeObserver(@bound_observer)
  object.removeObserver(self, forKeyPath: @attr_name)
  super
end