rubymotion/BubbleWrap — pull request #202 by ryansobol (2013-04-01 22:20:57+00:00)
Title: Add init wrappers for UIBarButtonItem

Great meeting you at #inspect, @mneorr.

For your consideration, this pull request adds idiomatic Ruby wrappers around the verbose Objective-C `init` methods of UIBarButtonItem.

---

### initWithBarButtonSystemItem:target:action:

#### Before

```ruby
def cancel_bar_button_item
UIBarButtonItem.alloc.initWithBarButtonSystemItem(UIBarButtonSystemItemCancel, target:self, action: :cancel)
end

def cancel
# ...
end
```

#### After

```ruby
def cancel_bar_button_item
UIBarButtonItem.system(:cancel) do
# ...
end
end
```

---

### initWithTitle:style:target:action:

#### Before

```ruby
def friends_bar_button_item
UIBarButtonItem.alloc.initWithTitle("Friends", style:UIBarButtonItemStyleBordered, target:self, action: :friends)
end

def friends
# ...
end
```

#### After

```ruby
def friends_bar_button_item
UIBarButtonItem.styled(:bordered, "Friends") do
# ...
end
end
```

---

### initWithImage:style:target:action:

#### Before

```ruby
def friends_bar_button_item
image = UIImage.imageNamed("glyph_friends")

UIBarButtonItem.alloc.initWithImage(image, style:UIBarButtonItemStylePlain, target:self, action: :friends)
end

def friends
# ...
end
```

#### After

```ruby
def friends_bar_button_item
image = UIImage.imageNamed("glyph_friends")

UIBarButtonItem.styled(:plain, image) do
# ...
end
end
```

---

### initWithImage:landscapeImagePhone:style:target:action:

#### Before

```ruby
def friends_bar_button_item
image = UIImage.imageNamed("glyph_friends")
image_landscape_phone = UIImage.imageNamed("glyph_friends_landscape_phone")

UIBarButtonItem.alloc.initWithImage(image, landscapeImagePhone:image_landscape_phone, style:UIBarButtonItemStylePlain, target:self, action: :friends)
end

def friends
# ...
end
```

#### After

```ruby
def friends_bar_button_item
image = UIImage.imageNamed("glyph_friends")
image_landscape_phone = UIImage.imageNamed("glyph_friends_landscape_phone")

UIBarButtonItem.styled(:plain, [image, image_landscape_phone]) do
# ...
end
end
```

**NOTE:** If's more pleasing to your sense of style, I'm open to refactoring the method signature like so:

```ruby
UIBarButtonItem.styled(:plain, image, image_landscape_phone) do
# ...
end
```

Personally, I prefer this cleaner syntax. Just be aware that it'll add some complexity to the method implementation. Let me know which you prefer.

---

### initWithCustomView:

#### Before

```ruby
def custom_bar_button_item
view = MyCustomView.alloc.init

UIBarButtonItem.alloc.initWithCustomView(view)
end

```

#### After

```ruby
def custom_bar_button_item
view = MyCustomView.alloc.init

UIBarButtonItem.custom(view)
end
```

**NOTE:** I made this wrapper for the sake of completeness and consistency.

---

Questions? Comments? Suggestions?