Class: UIAlertController

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#sugarcube_handlerObject

Returns the value of attribute sugarcube_handler.



2
3
4
# File 'lib/ios/sugarcube-factories/uialertcontroller.rb', line 2

def sugarcube_handler
  @sugarcube_handler
end

Class Method Details

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

Examples:

UIAlertController.alert(controller, "title",
  message: "help!",
  buttons: %w"Cancel OK No-way"
  ) { |pressed| } # pressed will be Cancel OK No-way


9
10
11
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
81
82
83
84
85
86
87
88
# File 'lib/ios/sugarcube-factories/uialertcontroller.rb', line 9

def self.alert(controller, 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

  style = options[:style] || UIAlertControllerStyleAlert
  style = style.uialertcontrollerstyle if style.respond_to?(:uialertcontrollerstyle)

  alert = self.alertControllerWithTitle(title, message: message, preferredStyle: style)

  buttons = options[:buttons]
  if buttons.nil?
    buttons = %w(OK Cancel)
  end

  if block
    alert.sugarcube_handler = SugarCube::UIAlertControllerCallbackHelper.new(block)
  end

  if buttons.is_a?(NSDictionary)
    button_keys = buttons.keys
    buttons = buttons.values
  else
    button_keys = buttons
  end
  buttons.each_with_index do |button, index|
    key = button_keys[index]
    case key
      when :cancel, 'Cancel'
        action_style = UIAlertActionStyleCancel
        button = 'Cancel' if button == :cancel
      when :destructive, 'Destructive'
        action_style = UIAlertActionStyleDestructive
        button = 'Destructive' if button == :destructive
      else
        action_style = UIAlertActionStyleDefault
    end
    label = NSBundle.mainBundle.localizedStringForKey(button, value: nil, table: nil)
    action = UIAlertAction.actionWithTitle(label,
                                           style:   action_style,
                                           handler: proc { |_|
                                             alert.sugarcube_handler.call(key) unless alert.sugarcube_handler.nil?
                                           })
    alert.addAction action
  end

  if style == UIAlertControllerStyleActionSheet and UIDevice.currentDevice.userInterfaceIdiom == :ipad.uidevice
    alert.modalPresentationStyle = UIModalPresentationPopover
    alert.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionAny

    from = options.fetch(:from, UIApplication.sharedApplication.windows[0])
    case from
      when CGRect
        view                                           = options.fetch(:view, UIApplication.sharedApplication.windows[0])
        alert.popoverPresentationController.sourceView = view
        alert.popoverPresentationController.sourceRect = from
      when UIBarButtonItem
        alert.popoverPresentationController.barButtonItem = from
      when UIView
        frame                                          = [[CGRectGetMidX(from.bounds), CGRectGetMidY(from.bounds)], [1.0, 1.0]]
        alert.popoverPresentationController.sourceView = from
        alert.popoverPresentationController.sourceRect = frame
      else
        raise "Unknown :from option #{from.inspect}"
    end
  end

  if options.fetch(:show, true)
    controller.presentViewController(alert, animated: true, completion: nil)
  end

  alert
end

Instance Method Details

#<<(title) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/ios/sugarcube-factories/uialertcontroller.rb', line 90

def <<(title)
  action = UIAlertAction.actionWithTitle(title,
                                         style:   UIAlertActionStyleDefault,
                                         handler: proc { |_|
                                           sugarcube_handler.call(title) unless sugarcube_handler.nil?
                                         })
  addAction action
end