Sha256: d47bdf3519d2a49e60c9b67d1367d2c455d81a2cb270fbcd32bbf0b4025b4e8c

Contents?: true

Size: 1.44 KB

Versions: 7

Compression:

Stored size: 1.44 KB

Contents

# Methods to retrieve a subview using the stylename as a key
# Kinda similar to jQuery-style $(el).find('stylename')
class UIView

  # get one subview by stylename or class.  If the receiver matches, it will be
  # returned
  # my_view.viewWithStylename :button => #<UIButton..>
  # my_view.viewWithStylename UIButton => #<UIButton..>
  def viewWithStylename name_or_class
    return self if self._teacup_check_stylename(name_or_class)

    view = subviews.find { |view| view._teacup_check_stylename(name_or_class) }
    return view if view

    # found_subview will get assigned to the view we want, but the subview is
    # what is returned.
    found_subview = nil
    view = subviews.find { |subview| found_subview = subview.viewWithStylename(name_or_class) }
    return found_subview if view

    nil  # couldn't find it
  end

  # get all subviews by stylename or class
  # my_view.viewsWithStylename :button => [#<UIButton..>, #<UIButton...>]
  # my_view.viewsWithStylename UIButton => [#<UIButton..>, #<UIButton...>]
  def viewsWithStylename name_or_class
    r = []
    r << self if self._teacup_check_stylename(name_or_class)

    subviews.each do |view|
      r << view if view._teacup_check_stylename(name_or_class)
      r.concat view.viewsWithStylename name_or_class
    end
    r
  end

  def _teacup_check_stylename(name_or_class)
    if name_or_class.is_a? Class
      self.is_a? name_or_class
    else
      self.stylename == name_or_class
    end
  end

end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
teacup-1.2.9 lib/teacup/z_core_extensions/ui_view_getters.rb
teacup-1.2.8 lib/teacup/z_core_extensions/ui_view_getters.rb
teacup-1.2.7 lib/teacup/z_core_extensions/ui_view_getters.rb
teacup-1.2.5 lib/teacup/z_core_extensions/ui_view_getters.rb
teacup-1.2.4 lib/teacup/z_core_extensions/ui_view_getters.rb
teacup-1.2.3 lib/teacup/z_core_extensions/ui_view_getters.rb
teacup-1.2.2 lib/teacup/z_core_extensions/ui_view_getters.rb