mirror of
https://github.com/zhigang1992/rmq.git
synced 2026-01-12 09:33:43 +08:00
Merge pull request #234 from infinitered/hide_keyboard
`rmq.app.hide_keyboard` to close #233
This commit is contained in:
@@ -1,4 +1,11 @@
|
||||
class MainController < UIViewController
|
||||
attr_accessor :keyboard_visible
|
||||
|
||||
def viewWillAppear(animated)
|
||||
# Adding these for tests
|
||||
NSNotificationCenter.defaultCenter.addObserver(self, selector:'handleKeyboardDidShow:', name:UIKeyboardDidShowNotification, object:nil)
|
||||
NSNotificationCenter.defaultCenter.addObserver(self, selector:'handleKeyboardWillHide:', name:UIKeyboardWillHideNotification, object:nil)
|
||||
end
|
||||
|
||||
def viewDidLoad
|
||||
super
|
||||
@@ -172,4 +179,12 @@ class MainController < UIViewController
|
||||
sender.resignFirstResponder
|
||||
true
|
||||
end
|
||||
|
||||
def handleKeyboardDidShow(notification)
|
||||
@keyboard_visible = true
|
||||
end
|
||||
|
||||
def handleKeyboardWillHide(notification)
|
||||
@keyboard_visible = false
|
||||
end
|
||||
end
|
||||
|
||||
@@ -32,6 +32,20 @@ module RubyMotionQuery
|
||||
UIApplication.sharedApplication.delegate
|
||||
end
|
||||
|
||||
# @return [UIApplication]
|
||||
def get
|
||||
UIApplication.sharedApplication
|
||||
end
|
||||
|
||||
# Returns boolean of success of hiding
|
||||
# Tested in the example app!
|
||||
# @return [Boolean]
|
||||
def hide_keyboard
|
||||
self.get.sendAction(:resignFirstResponder, to:nil, from:nil, forEvent:nil)
|
||||
end
|
||||
alias :resign_responders :hide_keyboard
|
||||
alias :end_editing :hide_keyboard
|
||||
|
||||
# @return [Symbol] Environment the app is running it
|
||||
def environment
|
||||
@_environment ||= RUBYMOTION_ENV.to_sym
|
||||
|
||||
37
spec/controllers/main_controller.rb
Normal file
37
spec/controllers/main_controller.rb
Normal file
@@ -0,0 +1,37 @@
|
||||
describe "MainController" do
|
||||
tests MainController
|
||||
|
||||
DELAY = 0.2
|
||||
|
||||
it 'starts with no keyboard shown' do
|
||||
controller.keyboard_visible.nil?.should == true
|
||||
controller.rmq.app.hide_keyboard.should == false
|
||||
end
|
||||
|
||||
it 'can hide the keyboard with rmq.app.hide_keyboard' do
|
||||
UIView.setAnimationsEnabled false
|
||||
|
||||
# force keyboard to show
|
||||
controller.rmq(:only_digits).get.becomeFirstResponder
|
||||
wait DELAY do
|
||||
controller.keyboard_visible.should == true
|
||||
end
|
||||
|
||||
# hide keyboard (have to wait for first delay to finish)
|
||||
wait DELAY * 2 do
|
||||
controller.rmq.app.hide_keyboard.should == true
|
||||
wait DELAY do
|
||||
controller.keyboard_visible.should == false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
it 'has aliases for rmq.app.hide_keyboard' do
|
||||
rmq.app.respond_to?(:resign_responders).should == true
|
||||
rmq.app.respond_to?(:end_editing).should == true
|
||||
controller.rmq.app.resign_responders.should == false
|
||||
controller.rmq.app.end_editing.should == false
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user