Search code examples
iosswiftuialertviewuialertcontrolleruiactionsheet

Check if UIAlertController action sheet is dismissed through cancel button?


I have implemented action sheet though alert controller. I want to display a button like cancel button with "Pay" Text written on it.

Issue is makeCall() function call when pay button is clicked , And when rest of the screen is tapped makeCall() function is called again.

How can I identify that action is called through pay button action or through Tapp on rest of the screen? I only want to make call to makeCall() function when pay button is tapped.

alertController = UIAlertController(title: "", message: nil, preferredStyle: .actionSheet)    

let cancelAction = UIAlertAction(title: "Pay", style: .cancel) { (UIAlertAction) in
    printLog("cancelAction")
    makeCall()
}

cancelAction.isEnabled = false
alertController.addAction(cancelAction)

self.present(alertController, animated: true) {}

enter image description here


Solution

  • Here, alert controller view userInteraction disable so when tap outside alert controller not close.

    You can do like this:

    self.present(alertController, animated: true){
        if let mainView = alertController.view.superview?.subviews[0]{
               mainView.isUserInteractionEnabled = false
         }
    }
    

    OR

    self.present(alertController, animated: true) {
       if let allContainerView = alertController.view.superview?.subviews{
           for myview in allContainerView{
               if (myview.gestureRecognizers != nil){
                     myview.isUserInteractionEnabled = false
               }
           }
        }
     }
    

    I hope it will work for you.