Sha256: 7e0496aca1a19365f53d0680df863e07ca86b1e2bffa3ed0a915e70f374cdb5c

Contents?: true

Size: 780 Bytes

Versions: 1

Compression:

Stored size: 780 Bytes

Contents

module MotionBindable::Strategies

  class UITextField < ::MotionBindable::Strategy

    include BW::KVO

    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

    def on_bound_change
      self.attribute = bound.text
    end

    def on_object_change
      @bound.text = attribute
    end

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

    # Text field takes precedence
    alias_method :refresh, :on_bound_change

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

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