Class: UIActionSheet

Inherits:
Object show all
Defined in:
lib/sugarcube-factories/uiactionsheet.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.alert(title, options = {}, &block) ⇒ Object

For the purposes of whether the cancel or success handler gets called, the first button is considered the 'cancel' button, the second button is the 'destructive' button, and the rest are plain old buttons.

If you use just one block, it will be used for all of the buttons.

Examples:

# use a different handler for each button type
UIActionSheet.alert("title",
  buttons: %w"Cancel Delete No-way",
  cancel: proc{ puts "nevermind" },
  destructive: proc{ puts "OHHH YEAAH!" },
  success: proc{ |pressed| puts "pressed: #{pressed}" },
  )
# use one handler for all buttons
UIActionSheet.alert("title", buttons: [...]) { |button| }


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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/sugarcube-factories/uiactionsheet.rb', line 19

def self.alert(title, options={}, &block)
  # create the delegate
  delegate = SugarCube::ActionSheetDelegate.new
  delegate.on_default = block
  delegate.on_success = options[:success]
  delegate.on_destructive = options[:destructive]
  delegate.on_cancel = options[:cancel]
  delegate.send(:retain)

  args = [title]            # initWithTitle:
  args << delegate          # delegate:

  buttons = []
  buttons.concat(options[:buttons]) if options[:buttons]

  if buttons.empty?
    # cancelButtonTitle:
    buttons << nil

    # destructiveButtonTitle
    buttons << nil

    # otherButtonTitles:
    buttons << 'OK'
  elsif buttons.length == 1 && (options[:cancel] || options[:destructive])
    raise 'If you only have one button, use a :success handler, not :cancel or :destructive'
  end

  # cancelButtonTitle:destructiveButtonTitle:otherButtonTitles:
  args.concat(buttons.map{ |s| s ? s.localized : nil })
  args << nil  # otherButtonTitles:..., nil

  # the button titles, mapped to how UIActionSheet orders them.  These are
  # passed to the success handler.
  buttons_mapped = {}
  if args[2] && args[3]  # cancel && destructive buttons
    buttons_mapped[0] = buttons[1]                   # destructiveIndex == 0, button == 1
    buttons_mapped[buttons.length - 1] = buttons[0]  # cancelIndex == last, button == 0
    # from first+1 to last-1
    buttons[2..-1].each_with_index do |button,index|
      buttons_mapped[index + 1] = button
    end
  elsif args[3]  # destructive button
    buttons_mapped[0] = buttons[1]                   # destructiveIndex == 0, button == 1
    # from first+1 to last-1
    buttons[2..-1].each_with_index do |button,index|
      buttons_mapped[index + 1] = button
    end
  elsif args[2]  # cancel button
    buttons_mapped[buttons.length - 2] = buttons[0]  # cancelIndex == last, button == 0
    buttons[2..-1].each_with_index do |button,index|
      buttons_mapped[index] = button
    end
  else
    buttons[2..-1].each_with_index do |button,index|
      buttons_mapped[index] = button
    end
  end
  delegate.buttons = buttons_mapped

  alert = self.alloc
  alert.send('initWithTitle:delegate:cancelButtonTitle:destructiveButtonTitle:otherButtonTitles:', *args)
  if options.key?(:style)
    style = options[:style]
    if style.respond_to?(:uiactionstyle)
      style = style.uiactionstyle
    end
    alert.actionSheetStyle = style
  end
  if options.fetch(:show, true)
    if options.key?(:from)
      from = options[:from]
    else
      from = UIApplication.sharedApplication.windows[0]
    end

    case from
    when CGRect
      view = UIApplication.sharedApplication.windows[0]
      alert.showInRect(from, inView: view, animated: true)
    when UIBarButtonItem
      alert.showFromBarButtonItem(from)
    when UIToolbar
      alert.showFromToolbar(from)
    when UITabBar
      alert.showFromTabBar(from)
    when UIView
      alert.showInView(from)
    else
      raise "Unknown :from option #{from.inspect}"
    end
  end
  alert
end

Instance Method Details

#<<(title) ⇒ Object



114
115
116
# File 'lib/sugarcube-factories/uiactionsheet.rb', line 114

def <<(title)
  addButtonWithTitle(title)
end