Sha256: f6d03e927348457a88d53d5ab86620506d06689f583552bd1a4445475fcdaed6

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 KB

Contents

module MotionBindable::Strategies

  class UITextField < ::MotionBindable::Strategy

    def start_observing_bound
      @bound_observer = 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
      NSNotificationCenter.defaultCenter.removeObserver(@bound_observer)
      object.removeObserver(self, forKeyPath: @attr_name)
      super
    end

    # NSKeyValueObserving Protocol

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

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
motion_bindable-0.2.2 lib/strategies/ui_text_field.rb