Class: UIActionSheet
Class Method Summary collapse
-
.alert(title, options = {}, &block) ⇒ Object
UIActionSheet.alert("title", # The first button is considered the 'cancel' button, for the purposes of # whether the cancel or success handler gets called, the second button is # the 'destructive' button, and the rest are plain old buttons. buttons: %w"Cancel OK No-way", cancel: proc{ puts "nevermind" }, destructive: proc{ puts "OHHH YEAAH!" }, success: proc{ |pressed| puts "pressed: #pressed" }, ).
Class Method Details
.alert(title, options = {}, &block) ⇒ Object
UIActionSheet.alert("title", # The first button is considered the 'cancel' button, for the purposes of # whether the cancel or success handler gets called, the second button is # the 'destructive' button, and the rest are plain old buttons. buttons: %w"Cancel OK No-way", cancel: proc{ puts "nevermind" }, destructive: proc{ puts "OHHH YEAAH!" }, success: proc{ |pressed| puts "pressed: #pressed" }, )
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/sugarcube/uiactionsheet.rb', line 12 def self.alert(title, ={}, &block) # create the delegate delegate = SugarCube::ActionSheetDelegate.new delegate.on_success = [:success] || block delegate.on_destructive = [:destructive] delegate.on_cancel = [:cancel] delegate.send(:retain) args = [title] # initWithTitle: args << delegate # delegate: = [] .concat([:buttons]) if [:buttons] if .empty? # cancelButtonTitle: is first, so check for cancel handler if [:cancel] << 'Cancel' else << nil end # destructiveButtonTitle, check for destructive handler if [:destructive] << 'Delete' else << nil end elsif .length == 1 and ([:cancel] or [:destructive]) raise 'If you only have one button, use a :success handler, not :cancel or :destructive' end # uses localized buttons in the actual alert if .length == 0 = [nil, nil] elsif .length == 1 << nil end last_index = .length - 1 offset = 0 = {} if [1] # destructive [0] = [1] offset += 1 else last_index -= 1 end if [0] # cancel [last_index] = [0] end [2..-1].each_with_index { |, index| [index + offset] = } # the button titles, mapped to how UIActionSheet orders them. These are passed to the success handler. delegate. = args.concat(.map{ |s| s ? s.localized : nil }) args << nil # otherButtonTitles:..., nil alert = self.alloc alert.send('initWithTitle:delegate:cancelButtonTitle:destructiveButtonTitle:otherButtonTitles:', *args) window = UIApplication.sharedApplication.keyWindow || UIApplication.sharedApplication.windows[0] alert.showInView(window) alert end |