Skip to content

Instantly share code, notes, and snippets.

@joshavant
Created October 8, 2015 23:29
Show Gist options
  • Select an option

  • Save joshavant/5e686636d267ca88f3a1 to your computer and use it in GitHub Desktop.

Select an option

Save joshavant/5e686636d267ca88f3a1 to your computer and use it in GitHub Desktop.
func alertControllerToSelectContactMethod() -> UIAlertController {
let controller = UIAlertController(title: "How do you want to contact \(firstName.value)?", message: "", preferredStyle: .ActionSheet)
let methodsAndHandlers = contactMethodsAndHandlers()
if (methodsAndHandlers.count == 0) {
let action = UIAlertAction(title: NSLocalizedString("No Contact Info Found", comment: ""), style: .Default, handler: nil)
action.enabled = false
controller.addAction(action)
controller.addAction(UIAlertAction(title: NSLocalizedString("Ok", comment: ""), style: .Default, handler: userSelectionCanceled))
} else {
methodsAndHandlers.forEach {
(methodAndHandler: (method: String, handler: ((UIAlertAction) -> Void))) in
controller.addAction(UIAlertAction(title: methodAndHandler.method, style: .Default, handler: methodAndHandler.handler))
}
controller.addAction(UIAlertAction(title: NSLocalizedString("Cancel", comment: ""), style: .Cancel, handler: userSelectionCanceled))
}
return controller
}
func contactMethodsAndHandlers() -> Array<(String, ((UIAlertAction) -> Void))> {
var methods: Array<(String, ((UIAlertAction) -> Void))> = []
phoneNumbers.forEach {
number in
methods.append((number, userSelectedPhoneNumber))
}
if(messageUIFactory.canSendMail()) {
emails.forEach {
email in
methods.append((email, userSelectedEmail))
}
}
return methods
}
// MARK: UIAlertAction Handlers
func userSelectedPhoneNumber(action: UIAlertAction!) {
// do things...
}
func userSelectedEmail(action: UIAlertAction!) {
// do things...
}
func userSelectionCanceled(action: UIAlertAction!) {
// do things...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment