UIViewController

class UIViewController : UIResponder, NSCoding, UIAppearanceContainer, UITraitEnvironment, UIContentContainer, UIFocusEnvironment

A UIViewController extension to support easily selecting an item from a list via an alert sheet.

  • Pop up an alert to let the user select an item from a list of provided items. The alert is dismissed an the completion handler called with the chosen item. If the user cancels without selecting an item, the completion handler will not be called.

    You may use this method with values of any type that are CustomStringConvertible. Example:

    let choices = [ "Chocolate", "Vanilla", "Banana" ]
      let flavor = chooseItemFromList(choices, prompt: "Choose Flavor") {
      if flavor == "Vanilla" {
        ...
      }
    }
    

    or:

    let numbers = [ 1, 3, 5, 7 ]
    let pick = chooseItemFromList(numbers, prompt: "Pick your favorite number") {
      print(pick * 2)
    }
    

    Declaration

    Swift

    public func chooseItemFromList<T : CustomStringConvertible>(_ items : [T],
                                                                prompt : String?,
                                                                completion: @escaping (_ item : T) -> Void)

    Parameters

    items

    the items to choose from

    prompt

    prompt given to the user

    completion

    the function to call on the chosen item