Sha256: 01ddb770da2fe01db3259a17149f72786a3a61911b9135897d6aef0401d7fe3e

Contents?: true

Size: 821 Bytes

Versions: 1

Compression:

Stored size: 821 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 unless bound.text.empty?
    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.1 lib/strategies/ui_text_field.rb