candostdagdeviren / CDAlertView

Highly customizable alertview and alert/notification/success/error/alarm popup written in Swift
https://candostdagdeviren.github.io/CDAlertView/
MIT License
1.14k stars 86 forks source link

Handle action on tap #39

Closed cdeck95 closed 6 years ago

cdeck95 commented 6 years ago

It isn't very clear in the documentation on how to handle different actions with a completion handler. Can someone please let me know how I can detect whether my "done action" or "cancel action" buttons are hit? Code is below. Thanks!

`let alert = CDAlertView(title: "Deleting All Accounts", message: "Are you sure you wish to delete all accounts?", type: .warning)

let doneAction = CDAlertViewAction(title: "Sure! 💪") alert.add(action: doneAction) let nevermindAction = CDAlertViewAction(title: "Nevermind 😬") alert.add(action: nevermindAction) alert.show()`

candostdagdeviren commented 6 years ago

Hello,

Thanks for letting me know that action documentation isn't clear.

When you look at the code under Advanced Action Initialization in Readme, you'll see there is a handler parameter. You can pass a closure to handle your tap on specified to the action button.

I'll try to update the documentation for actions soon.

cdeck95 commented 6 years ago

Hi,

Thanks for the help! After re-reading, it makes perfect sense and I was able to create the handler. I must have been staring at my screen too long yesterday because idk how I missed that!

For anyone else who finds this issue, please see how I handled the code below:

@IBAction func buttonClicked(_ sender: Any) {
        let alert = CDAlertView(title: "Title", message: "Confirming?", type: .warning)
        let doneAction = CDAlertViewAction(title: "Sure! 💪",
                                           font: UIFont.systemFont(ofSize: 17),
                                           textColor: UIColor(red: 27 / 255, green: 169 / 255, blue: 225 / 255, alpha: 1),
                                           backgroundColor: nil,
                                           handler: { action in self.doSomething()})
        alert.add(action: doneAction)
        let nevermindAction = CDAlertViewAction(title: "Nevermind 😬")
        alert.add(action: nevermindAction)
        alert.show()
    }

func doSomething(){
           //do something
    }