diff --git a/README.md b/README.md index 7634376..fe157b6 100755 --- a/README.md +++ b/README.md @@ -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 diff --git a/motion/ruby_motion_query/actions.rb b/motion/ruby_motion_query/actions.rb index d48588e..bd82343 100644 --- a/motion/ruby_motion_query/actions.rb +++ b/motion/ruby_motion_query/actions.rb @@ -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 diff --git a/spec/actions.rb b/spec/actions.rb index edec914..9f48b49 100644 --- a/spec/actions.rb +++ b/spec/actions.rb @@ -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