Class: MotionBindable::Strategies::UITextField

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

Instance Attribute Summary

Attributes inherited from MotionBindable::Strategy

#bound, #object

Instance Method Summary collapse

Methods inherited from MotionBindable::Strategy

#attribute, #attribute=, #bind, find_by_reference, #initialize, register_strategy

Constructor Details

This class inherits a constructor from MotionBindable::Strategy

Instance Method Details

#on_bindObject



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/strategies/ui_text_field.rb', line 7

def on_bind
  refresh

  # Observe the bound object
  NSNotificationCenter.defaultCenter.addObserver(
    self, selector: :on_bound_change,
    name: UITextFieldTextDidChangeNotification, object: bound
  )

  # Observe the attribute
  observe(object, @attr_name) { |_, _| on_object_change }
end

#on_bound_changeObject Also known as: refresh



20
21
22
# File 'lib/strategies/ui_text_field.rb', line 20

def on_bound_change
  self.attribute = bound.text
end

#on_object_changeObject



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

def on_object_change
  @bound.text = attribute
end

#unbindObject



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

def unbind
  App.notification_center.unobserve(@bound_observer)
  unobserve(object, @attr_name)
end