Class: SugarCube::AlertViewDelegate

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#buttonsObject

Returns the value of attribute buttons.



82
83
84
# File 'lib/sugarcube-factories/uialertview.rb', line 82

def buttons
  @buttons
end

#on_cancelObject

Returns the value of attribute on_cancel.



83
84
85
# File 'lib/sugarcube-factories/uialertview.rb', line 83

def on_cancel
  @on_cancel
end

#on_defaultObject

Returns the value of attribute on_default.



85
86
87
# File 'lib/sugarcube-factories/uialertview.rb', line 85

def on_default
  @on_default
end

#on_successObject

Returns the value of attribute on_success.



84
85
86
# File 'lib/sugarcube-factories/uialertview.rb', line 84

def on_success
  @on_success
end

Instance Method Details

#alertView(alert, didDismissWithButtonIndex: index) ⇒ Object



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
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/sugarcube-factories/uialertview.rb', line 87

def alertView(alert, didDismissWithButtonIndex:index)
  cancel_handler = on_cancel || on_default
  success_handler = on_success || on_default
  handler = nil

  if index == alert.cancelButtonIndex
    handler = cancel_handler
  else
    handler = success_handler
  end

  if handler
    if handler.arity == 0
      args = []
    else
      # construct all the possible arguments you could send
      args = [buttons[index]]
      # add the first input if this is not the default
      if alert.alertViewStyle != UIAlertViewStyleDefault
        args << alert.textFieldAtIndex(0).text
      end
      # add the second one if this is a login+password input
      if alert.alertViewStyle == UIAlertViewStyleLoginAndPasswordInput
        args << alert.textFieldAtIndex(1).text
      end

      # and maybe you want the index, too
      args << index

      # but only send the ones they asked for
      args = args[0...handler.arity]
    end
    handler.call(*args)
  end

  self.send(:autorelease)
end