Permalink
Browse files

Properly handle multiple targets per event (Closes #124, Closes #132)

  • Loading branch information...
1 parent 3f96fa0 commit 5aa044c351b9174a921a3418a036161beb01b024 @dmarkow dmarkow committed Sep 5, 2012
Showing with 50 additions and 12 deletions.
  1. +12 −1 CHANGELOG.md
  2. +10 −3 motion/ui/ui_control.rb
  3. +28 −8 spec/motion/core/ui_control_spec.rb
View
@@ -1,6 +1,17 @@
+## 1.1.4 (Unreleased)
+
+[Commit history](https://github.com/rubymotion/BubbleWrap/compare/v1.1.3...master)
+
+* Fixed a problem with `when` events not properly handling multiple targets per event. Now defaults to one target per event with an option to append multiple targets.
+
+## 1.1.3
+
+[Commit history](https://github.com/rubymotion/BubbleWrap/compare/v1.1.2...v1.1.3)
+
+
## 1.1.2
-[Commit history](https://github.com/rubymotion/BubbleWrap/compare/v1.1.0...v1.1.1)
+[Commit history](https://github.com/rubymotion/BubbleWrap/compare/v1.1.1...v1.1.2)
* Fixed a problem with the load path.
* Added `format:` to the HTTP wrapper with [5 supported formats supported](https://github.com/rubymotion/BubbleWrap/pull/109) that sets the content type accordingly.
View
@@ -1,7 +1,14 @@
module UIControlWrap
- def when(events, &block)
+ def when(events, options={}, &block)
@callback ||= {}
- @callback[events] = block
- addTarget(@callback[events], action:'call', forControlEvents: events)
+ @callback[events] ||= []
+
+ unless options[:append]
+ @callback[events] = []
+ removeTarget(nil, action: nil, forControlEvents: events)
+ end
+
+ @callback[events] << block
+ addTarget(@callback[events].last, action:'call', forControlEvents: events)
end
end
@@ -1,16 +1,36 @@
describe "UIControlWrap" do
+ describe 'UIButton' do
+ before do
+ @button = UIButton.buttonWithType(UIButtonTypeRoundedRect)
+ @touched = []
+ @button.when(UIControlEventTouchUpInside) do
+ @touched << 'for the very first time'
+ end
+ end
+
+ it "supports the 'when' event handler for UIButton" do
+ @button.sendActionsForControlEvents(UIControlEventTouchUpInside)
+ @touched.should == ['for the very first time']
+ end
+
+ it "replaces the target for a given control event by default" do
+ @button.when(UIControlEventTouchUpInside) do
+ @touched << 'touched'
+ end
+ @button.sendActionsForControlEvents(UIControlEventTouchUpInside)
+ @touched.should == ['touched']
+ end
- it "should support the 'when' event handler" do
- button = UIButton.buttonWithType(UIButtonTypeRoundedRect)
- touched = nil
- button.when(UIControlEventTouchUpInside) do
- touched = 'for the very first time'
+ it "allows multiple targets for a given control event if specified" do
+ @button.when(UIControlEventTouchUpInside, append: true) do
+ @touched << 'touched'
+ end
+ @button.sendActionsForControlEvents(UIControlEventTouchUpInside)
+ @touched.should == ['for the very first time', 'touched']
end
- button.sendActionsForControlEvents(UIControlEventTouchUpInside)
- touched.should == 'for the very first time'
end
- it "should support the 'when' event handler for UISlider" do
+ it "supports the 'when' event handler for UISlider" do
button = UISlider.alloc.init
changed = nil
button.when(UIControlEventValueChanged) do

0 comments on commit 5aa044c

Please sign in to comment.