Skip to content

Instantly share code, notes, and snippets.

@sxross
Created August 12, 2012 17:15
Show Gist options
  • Save sxross/3333063 to your computer and use it in GitHub Desktop.
Save sxross/3333063 to your computer and use it in GitHub Desktop.
UIToolbar Subclass to Include Save and Done Buttons
class SaveCancelToolbar < UIToolbar
attr_writer :delegate
def initWithFrame frame, andTitle: title
super
items = []
spacer = UIBarButtonItem.alloc.initWithBarButtonSystemItem(UIBarButtonSystemItemFlexibleSpace, target:nil, action:nil)
@cancel_button = UIBarButtonItem.alloc.initWithBarButtonSystemItem(UIBarButtonSystemItemCancel, target:self, action:'on_cancel:')
@save_button = UIBarButtonItem.alloc.initWithBarButtonSystemItem(UIBarButtonSystemItemSave, target:self, action:'on_save:')
@title_label = UILabel.alloc.initWithFrame([[0.0, 11], [Device.screen.width, 21]])
@title_label.setFont UIFont.fontWithName("Helvetica-Bold", size:18)
@title_label.backgroundColor = UIColor.clearColor
@title_label.setTextColor UIColor.grayColor
@title_label.text = title
@title_label.textAlignment = UITextAlignmentCenter
@title = UIBarButtonItem.alloc.initWithCustomView @title_label
items << @cancel_button
items << spacer
items << @title
items << spacer
items << @save_button
self.setItems items, animated:true
self
end
def enable_save
@save_button.enabled = true
end
def disable_save
@save_button.enabled = false
end
def enable_cancel
@cancel_button.enabled = true
end
def disable_cancel
@cancel_button.enabled = false
end
def on_cancel(sender)
@delegate.on_cancel(self)
end
def on_save(sender)
@delegate.on_save(self)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment