Skip to content

Instantly share code, notes, and snippets.

@thinkclay
Created April 12, 2013 13:06
Show Gist options
  • Save thinkclay/5371862 to your computer and use it in GitHub Desktop.
Save thinkclay/5371862 to your computer and use it in GitHub Desktop.
RubyMotion example of keyboard handlers
Teacup::Stylesheet.new :create_screen do
# Input Fields
style :input_text_wrapper,
left: 24,
image: UIImage.imageNamed('ui-textfield-normal.png'),
userInteractionEnabled: true,
width: 249
style :input_text_type,
extends: :input_text_wrapper,
top: 50
style :input_text_name,
extends: :input_text_wrapper,
constraints: [
constrain(:top).equals(:input_text_type, :bottom).plus(15)
]
style :input_text_location,
extends: :input_text_wrapper,
constraints: [
constrain(:top).equals(:input_text_name, :bottom).plus(15)
]
style :input_text,
color: BubbleWrap.rgb_color(118, 90, 59),
height: 21,
left: 8,
placeholder: 'Nothing Set',
top: 5,
width: 237
style :challenge_type,
extends: :input_text,
placeholder: 'Challenge Type'
style :challenge_name,
extends: :input_text,
placeholder: 'Name of the Challenge'
style :challenge_location,
extends: :input_text,
placeholder: 'Start Location'
end
class CreateViewController < UIViewController
include GM::KeyboardHandler
stylesheet :create_screen
# Create our parent declarations, instantiate, and set properties
def loadView
self.view = UIView.new
@create_challenge = UIScrollView.new
# A nice handy function from @colinta which will intercept our keyboard delegation
# and do work for us, like making the scroll area larger to accommodate overlap and such
prepare_keyboard_handler(@create_challenge)
end
# I still need to figure out if it's going to be best to leave this logic here,
# where I create multiple child views and attach them to @pagination ... OR if
# these should be moved out into their own controllers.. but then how will
# keyboard delegation and other master -> child properties get passed?
layout :root do
# assign our input fields to an array so we can loop through
# and manage keyboard actions
@input_fields = [];
subview(UIImageView, :input_text_type) do
@input_fields << subview(UITextField, :challenge_type)
end
subview(UIImageView, :input_text_name) do
@input_fields << subview(UITextField, :challenge_name)
end
subview(UIImageView, :input_text_location) do
@input_fields << subview(UITextField, :challenge_location)
end
end
# As the view is actually being called, lets attach our listeners
def viewWillAppear(animated)
super
keyboard_handler_start
@input_fields.each { |field|
field.delegate = self
}
self.view.when_tapped do
@input_fields.each { |field|
field.resignFirstResponder
}
end
end
# Remove the keyboard listener
def viewWillDisappear(animated)
super
keyboard_handler_stop
end
def textFieldShouldReturn(textfield)
index = @input_fields.index(textfield)
if index == @input_fields.length - 1
textfield.resignFirstResponder
else
@input_fields[index + 1].becomeFirstResponder
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment