Skip to content

Instantly share code, notes, and snippets.

@alecdoconnor
Last active November 27, 2017 02:04
Show Gist options
  • Save alecdoconnor/465b34c14880117a8ac28a60205df7de to your computer and use it in GitHub Desktop.
Save alecdoconnor/465b34c14880117a8ac28a60205df7de to your computer and use it in GitHub Desktop.
Extending UIAlertController for cleaner code
extension UIAlertController {
func addActions(_ actions: [UIAlertAction]) {
for action in actions {
self.addAction(action)
}
}
}
//
// ViewController.swift
// testAlertActionExtension
//
// Created by Alec O'Connor on 11/26/17.
// Copyright © 2017 Alec O'Connor. All rights reserved.
//
// File is for demonstration
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
displayAlert()
}
func displayAlert() {
let alertController = UIAlertController(title: "Are you sure", message: "Do you want to delete everything before saving?", preferredStyle: .alert)
let action1 = UIAlertAction(title: "Save First", style: .default, handler: nil)
let action2 = UIAlertAction(title: "Delete Everything", style: .destructive, handler: nil)
let action3 = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
// alertController.addAction(action1)
// alertController.addAction(action2)
// alertController.addAction(action3)
alertController.addActions([action1, action2, action3])
present(alertController, animated: true, completion: nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment