How to create uialertcontroller in global swift

后端 未结 12 1695
情深已故
情深已故 2020-12-05 12:09

I\'m trying to create uialertcontroller in Config.swift file as follow.

static func showAlertMessage(titleStr:String, messageStr:St         


        
12条回答
  •  失恋的感觉
    2020-12-05 12:43

    What I'm using based on the solution by @William Hu:

    func popup(caller:UIViewController, style:UIAlertControllerStyle? = UIAlertControllerStyle.alert,
            title:String, message:String, buttonTexts:[String], buttonStyles:([UIAlertActionStyle?])? = nil,
            handlers:[((UIAlertAction) -> Void)?], animated:Bool? = nil, completion: (() -> Void)? = nil) {
        let alert = UIAlertController(title: title, message: message, preferredStyle: style!)
        for i in 0..= buttonStyles!.count || buttonStyles![i] == nil ?
                    UIAlertActionStyle.default : buttonStyles![i]!),
                handler: (i >= handlers.count || handlers[i] == nil ? nil : handlers[i]!)))
        }
        caller.present(alert, animated: animated != nil ? animated! : true, completion: completion)
    }
    
    1. Single function gives Alert by default and can optionally be used for ActionSheet.
    2. Arrays buttonTexts, buttonStyles and handlers can be of unequal sizes as per requirement.
    3. Actions can be styled.
    4. Animated can be specified.
    5. Optional block can be specified to be executed when presentation finishes.

    Usage:

    popup(caller: self, style: UIAlertControllerStyle.alert,
            title: "Title", message: "Message",
            buttonTexts: ["Destructive", "Cancel", "OK"],
            buttonStyles: [UIAlertActionStyle.destructive, UIAlertActionStyle.cancel],
            handlers: [nil], animated: false)
    

提交回复
热议问题