Skip to content

Instantly share code, notes, and snippets.

@michael-erasmus
Created September 2, 2012 07:27
Show Gist options
  • Save michael-erasmus/3595583 to your computer and use it in GitHub Desktop.
Save michael-erasmus/3595583 to your computer and use it in GitHub Desktop.
class MyViewController < UIViewController
include ViewTags
#by convention, these views will have tags that correspond to the order you specify them in
# :date_label:1, :name_label:2
has_view :date_label, :name_label
def loadView
views = NSBundle.mainBundle.loadNibNamed "myview", owner:self, options:nil
self.view = views[0]
end
def viewDidLoad
@date_label = get_view(:date_label)
end
end
module ViewTags
def self.included(base)
base.extend ClassMethods
end
def get_view_with_name(name)
view.view_with_tag(self.class.getTagFor(name))
end
def get_view_in(parent,with_name:name)
parent.view_with_tag(self.class.get_tag_for(name))
end
module ClassMethods
def has_view(name, with_tag:tag)
@view_tags ||= {}
@view_tags[name] = tag
end
def has_views(*names)
names.each_with_index do |name, tag|
has_view name, with_tag: tag + 1
end
end
def get_tag_for(name)
@view_tags[name]
end
end
end
@Dan2552
Copy link

Dan2552 commented Sep 13, 2012

Any idea why I might be getting the following error?

Terminating app due to uncaught exception 'NameError', reason: 'uninitialized constant CalculatorViewController::ViewTags (NameError)

app/calculate_view_controller.rb

class CalculatorViewController < UIViewController
  include ViewTags
end

and then your view_tags file also in app. Am I missing something?

@michael-erasmus
Copy link
Author

Sorry I only saw your comment now.

You probably need to setup compiler dependencies between your calculate_view_controller.rb file and the view_tags.rb file. By default RM will just compile them alphabetically. You can either do this manually in your Rakefile:

Motion::Project::App.setup do |app|
   # ...
    app.files_dependencies 'calculate_view_controller.rb' => 'lib/view_tags.rb'
end

Or you can use my solution do do this automatically:

https://gist.github.com/3698332

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment