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
|
# File 'lib/ios/sugarcube-factories/uialertview.rb', line 21
def self.alert(title, options={}, more_options={}, &block)
if title.is_a?(NSDictionary)
options = title
title = options[:title]
message = options[:message]
elsif options.is_a? String
message = options
options = more_options
else
message = options[:message]
end
delegate = SugarCube::AlertViewDelegate.new
delegate.on_success = options[:success]
delegate.on_cancel = options[:cancel]
delegate.on_default = block
delegate.send(:retain)
args = [title] args << message args << delegate
buttons = (options[:buttons] || []).freeze
if buttons.empty?
buttons = []
if options[:cancel]
buttons << "Cancel"
end
if buttons.empty?
args << nil buttons << "OK" elsif options[:success]
buttons << "OK"
end
elsif buttons.length == 1 && options[:cancel]
raise "If you only have one button, use a :success handler, not :cancel (and definitely not BOTH)"
end
if buttons.is_a?(NSDictionary)
button_keys = buttons.keys
if buttons.key?(:cancel)
args << (buttons[:cancel] && NSBundle.mainBundle.localizedStringForKey(buttons[:cancel], value: nil, table: nil))
else
args << nil
end
args.concat(buttons.select { |k, m| k != :cancel }.map { |k, m| m && NSBundle.mainBundle.localizedStringForKey(m, value: nil, table: nil) })
else
button_keys = buttons
args.concat(buttons.map { |m| m && NSBundle.mainBundle.localizedStringForKey(m, value: nil, table: nil) })
end
delegate.buttons = button_keys
args << nil
alert = self.alloc
alert.send("initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles:", *args)
if options.key?(:style)
style = options[:style]
style = style.uialertstyle if style.respond_to?(:uialertstyle)
alert.alertViewStyle = style
end
if options.fetch(:show, true)
alert.show
end
alert
end
|