Stop Dismissing UIViewController While User finished Action in UIAlertControl

56 Views Asked by At

How to handle action of alert controller button tapped, then continue dismissing of current view controller?

Some Peace of Code:

 internal func dismissPasscodeLock(_ lock: PasscodeLockType, completionHandler: (() -> Void)? = nil) {
   
    // create the alert
       let alert = UIAlertController(title: "TouchID", message: "Message?", preferredStyle: UIAlertController.Style.alert)

       // add the actions (buttons)
    alert.addAction(UIAlertAction(title: "Yes", style: UIAlertAction.Style.default, handler: {action in self.pressedAction()}))
       alert.addAction(UIAlertAction(title: "No", style: UIAlertAction.Style.cancel, handler: nil))
    
    let currentTopVC: UIViewController? = self.currentTopViewController()
    currentTopVC?.present(alert, animated: true, completion: nil)
    
    // if presented as modal
    if presentingViewController?.presentedViewController == self {
        dismiss(animated: animateOnDismiss) { [weak self] in
            
            self?.dismissCompletionCallback?()
            completionHandler?()
        }
    } else {
        // if pushed in a navigation controller
        _ = navigationController?.popViewController(animated: animateOnDismiss)
        dismissCompletionCallback?()
        completionHandler?()
    }
}

func pressedAction(){}

func currentTopViewController() -> UIViewController {
    var topVC: UIViewController? = UIApplication.shared.delegate?.window??.rootViewController
    while ((topVC?.presentedViewController) != nil) {
        topVC = topVC?.presentedViewController
    }
    return topVC!
}

Thanks in advance

1

There are 1 best solutions below

0
Jawad Ali On BEST ANSWER

You can create a function to get output of your alert with completion handler like this

    func showAlert(completion: @escaping () -> Void) {
     let alert = UIAlertController(title: "TouchID", message: "Message?", preferredStyle: UIAlertController.Style.alert)
    
           // add the actions (buttons)
        alert.addAction(UIAlertAction(title: "Yes", style: UIAlertAction.Style.default, handler: {action in 
              self.pressedAction()
              completion()
            }))
       alert.addAction(UIAlertAction(title: "No", style: UIAlertAction.Style.cancel, handler: {
             completion()
           }))
        
        let currentTopVC: UIViewController? = self.currentTopViewController()
        currentTopVC?.present(alert, animated: true, completion: nil)
    }

how to use

internal func dismissPasscodeLock(_ lock: PasscodeLockType, completionHandler:@escaping (() -> Void)? = nil) {
   
    // create the alert
       showAlert {
            
        
    // if presented as modal
    if presentingViewController?.presentedViewController == self {
        dismiss(animated: animateOnDismiss) { [weak self] in
            
            self?.dismissCompletionCallback?()
            completionHandler?()
        }
    } else {
        // if pushed in a navigation controller
        _ = navigationController?.popViewController(animated: animateOnDismiss)
        dismissCompletionCallback?()
        completionHandler?()
    }
  }
}