Sha256: b9a9508cbed35caf9a1aa260b7956a4f39a168cc6648e78c71e152f75ebec851

Contents?: true

Size: 1 KB

Versions: 2

Compression:

Stored size: 1 KB

Contents

module MotionBindable::Strategies

  class UITextField < ::MotionBindable::Strategy

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

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

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

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

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

    # NSKeyValueObserving Protocol

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

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
motion_bindable-0.2.1 lib/strategies/ui_text_field.rb
motion_bindable-0.2.0 lib/strategies/ui_text_field.rb