Sha256: 12142150aebad2e9a39fc72b040148cfd1459bd3a5f8cc49dcd5a8e94d857435

Contents?: true

Size: 1.06 KB

Versions: 3

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

3 entries across 3 versions & 1 rubygems

Version Path
motion_bindable-0.2.5 lib/strategies/ui_text_field.rb
motion_bindable-0.2.4 lib/strategies/ui_text_field.rb
motion_bindable-0.2.3 lib/strategies/ui_text_field.rb