UIAlertController

  • Declaration

    Swift

    public func present(in vc: UIViewController, animated flag: Bool)
  • Declaration

    Swift

    public func present(in vc: UIViewController)
  • A shorthand of addAction(UIAlertAction(title: title, style: .default, handler: handler)).

    let alert = // create alert or actionSheet
    alert.default("title")
    // or
    alert.default("title"){ action in
      // do something.
    }
    

    Declaration

    Swift

    public func `default`(_ title: String?, handler: ((UIAlertAction) -> Void)? = nil) -> Self

    Parameters

    title

    The text for the action button title.

    handler

    A block to execute when the user selects the action.

    Return Value

    An instance of itself.

  • A shorthand of addAction(UIAlertAction(title: title, style: .default, handler: handler)). This method is useful for the .alert style. It contains TextFields in the handler.

    let alert = // Create an alert.
    alert.default("title")
    // or
    alert.default("title"){ (action, textFields) in
      // do something.
    }
    

    Declaration

    Swift

    public func `default`(_ title: String?, handler: ((UIAlertAction, [UITextField]?) -> Void)?) -> Self

    Parameters

    title

    The text for the action button title.

    handler

    A block to execute when the user selects the action.

    Return Value

    An instance of itself.

  • A shorthand of default("OK", handler: handler).

    let alert = // Create an alert or an actionSheet.
    alert.ok()
    // or
    alert.ok { action in
      // do something.
    }
    

    Declaration

    Swift

    public func ok(_ handler: ((UIAlertAction) -> Void)? = nil) -> Self

    Parameters

    handler

    A block to execute when the user selects the action.

    Return Value

    An instance of itself.

  • A short hand of default("OK", handler: handler). This method is useful for the .alert style. It contains TextFields in the handler.

    let alert = // Create an alert.
    alert.ok()
    // or
    alert.ok { (action, textFields) in
      // do something.
    }
    

    Declaration

    Swift

    public func ok(_ handler: ((UIAlertAction, [UITextField]?) -> Void)?) -> Self

    Parameters

    handler

    A block to execute when the user selects the action.

    Return Value

    An instance of itself.

  • A shorthand of addAction(UIAlertAction(title: title, style: .cancel, handler: handler)).

    let alert = // Create an alert or an actionSheet.
    alert.cancel()
    // or
    alert.cancel("title")
    // or
    alert.cancel("title") { action in
      // do something.
    }
    

    Declaration

    Swift

    public func cancel(_ title: String? = "Cancel", handler: ((UIAlertAction) -> Void)? = nil) -> Self

    Parameters

    title

    The text for the action button title. The default value is Cancel.

    handler

    A block to execute when the user selects the action.

    Return Value

    An instance of itself.

  • A shorthand of addAction(UIAlertAction(title: title, style: .cancel, handler: handler)). This method is useful for the .alert style. It contains TextFields in the handler.

    let alert = // Create an alert.
    alert.cancel()
    // or
    alert.cancel("title")
    // or
    alert.cancel("title") { (action, textFields) in
      // do something.
    }
    

    Declaration

    Swift

    public func cancel(_ title: String? = "Cancel", handler: ((UIAlertAction, [UITextField]?) -> Void)?) -> Self

    Parameters

    title

    The text for the action button title. The default value is Cancel.

    handler

    A block to execute when the user selects the action.

    Return Value

    An instance of itself.

  • A shorthand of addAction(UIAlertAction(title: title, style: .destructive, handler: handler)).

    Declaration

    Swift

    public func destructive(_ title: String?, handler: ((UIAlertAction) -> Void)? = nil) -> Self

    Parameters

    title

    The text for the action button title.

    handler

    A block to execute when the user selects the action.

    Return Value

    An instance of itself.

  • A shorthand of addAction(UIAlertAction(title: title, style: .destructive, handler: handler)). This method is useful for the .alert style. It contains TextFields in the handler.

    Declaration

    Swift

    public func destructive(_ title: String?, handler: ((UIAlertAction, [UITextField]?) -> Void)?) -> Self

    Parameters

    title

    The text for the action button title.

    handler

    A block to execute when the user selects the action.

    Return Value

    An instance of itself.

  • Present the alert on a given view controller.

    let alert = // Create an alert or an actionSheet.
    alert.present(in: viewController)
    // or
    alert.present(in: self, animated: true) {
       print("do some thing after the presentation finishes.")
    }
    

    Declaration

    Swift

    @objc public func present(in vc: UIViewController, animated flag: Bool = true, completion: (() -> Void)? = nil)

    Parameters

    vc

    The parent view controller.

    flag

    Pass true to animate the presentation; otherwise, pass false.

    completion

    The block to execute after the presentation finishes.

  • A shorthand of addTextField(configurationHandler:)

    let alert = // Create an alert.
    alert.textField { textField in
     configure textField.
    }
    

    Declaration

    Swift

    public func textField(_ configurationHandler: ((UITextField) -> Void)? = nil) -> Self

    Parameters

    configurationHandler

    A block for configuring the text field prior to displaying the alert.

  • A shorthand of addTextField(configurationHandler:). This method is useful if you only want to set default text or placeholders in a TextField.

    let alert = // Create an alert.
    alert.textField("default text", placeholder: "placeholder")
    

    Declaration

    Swift

    public func textField(_ defaultText: String? = nil, placeholder: String? = nil) -> Self

    Parameters

    defaultText

    The text for TextField.

    placeholder

    the text for TextField placeholder.