Added focus action

This commit is contained in:
Todd Werth
2013-08-05 11:47:02 -07:00
parent 8cc1680c5b
commit a6f32baaba
3 changed files with 49 additions and 1 deletions

View File

@@ -298,6 +298,7 @@ rmq(my_view).hide
rmq(my_view).show
rmq(my_view).toggle
rmq(my_view).toggle_enabled
rmq(my_text_field).focus # or .become_first_responder
```
### Subviews

View File

@@ -20,6 +20,18 @@ module RubyMotionQuery
self
end
# Sets the last selected view as the first responder
#
# @example
# rmq(my_view).next(UITextField).focus
def focus
unless RMQ.is_blank?(selected)
selected.last.becomeFirstResponder
end
self
end
alias :become_first_responder :focus
def hide
selected.each { |view| view.hidden = true }
self

View File

@@ -2,7 +2,7 @@ describe 'actions' do
before do
@vc = UIViewController.alloc.init
@viewq = @vc.rmq.append(UIView)
@sub_viewq = @vc.rmq(@viewq).append(UIView)
@sub_viewq = @viewq.append(UIView)
end
it 'should hide, show, and toggle a view' do
@@ -52,6 +52,41 @@ describe 'actions' do
end
end
describe 'focus' do
before do
window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)
@vc = UIViewController.alloc.initWithNibName(nil, bundle: nil)
window.rootViewController = @vc
window.makeKeyAndVisible
@tf_0 = @vc.rmq.append(UITextField).style do |st|
st.frame = {l: 10, t: 10, w: 100, h: 30}
end.get
@tf_1 = @vc.rmq.append(UITextField).style do |st|
st.frame = {l: 10, t: 50, w: 100, h: 30}
end.get
end
it 'should set focus to a selected view' do
@tf_1.becomeFirstResponder.should == true
@tf_1.isFirstResponder.should == true
@tf_0.isFirstResponder.should == false
@vc.rmq(@tf_0).focus.is_a?(RubyMotionQuery::RMQ).should == true
@tf_1.isFirstResponder.should == false
@tf_0.isFirstResponder.should == true
end
it 'should focus only the last selected view, if many views are selected' do
@tf_0.becomeFirstResponder.should == true
@tf_1.isFirstResponder.should == false
@tf_0.isFirstResponder.should == true
@vc.rmq(@tf_0, @tf_1).become_first_responder
@tf_1.isFirstResponder.should == true
@tf_0.isFirstResponder.should == false
end
end
it 'should set attributes on a view' do
1.should == 1
# TODO