Alexsander Alexsander - 27 days ago 6
iOS Question

Dissmiss completion block doesn't work. Swift

I have

PhotoBrowser
(it inherits from
UIViewController
) as a
presentedViewController
. I present it from
UITableViewController
. The
PhotoBrowser
presents
actionMainController
(it inherits from
UIAlertController
) when I tapped on button. I want to call another
UIAlertController
, when I select one of the actions in the actionMainController and display the second
UIAlertController
immediately after its dismissed but completion block doesn't work. I want to add that dismiss the
actionMainController
succeeds, it works

class PhotoBrowser: SKPhotoBrowser {

// MARK: - Controllers

private var actionMainController = UIAlertController()

// MARK: - Lifecycle

override func viewDidLoad() {
super.viewDidLoad()
}


// MARK: - override var

override var prefersStatusBarHidden: Bool {
return true
}

// MARK: - functions

func editAvatar() {
debugPrint("removePhoto")

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

actionMainController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

let deleteAction = UIAlertAction(title: "Delete Photo", style: .destructive) { (action) in
self.deletePhoto()
}
let takePhoto = UIAlertAction(title: "Take Photo", style: .default) { (action) in

}
let choosePhoto = UIAlertAction(title: "Choose Photo", style: .default) { (action) in

}
actionMainController.addAction(deleteAction)
actionMainController.addAction(takePhoto)
actionMainController.addAction(choosePhoto)

present(actionMainController, animated: true, completion: nil)
}

private func deletePhoto() {
actionMainController.dismiss(animated: true) {
// TODO: it
// self.showDeleteActionSheet()
}
showDeleteActionSheet()
}

private func showDeleteActionSheet() {
let deleteActionController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
deleteActionController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
let deleteAction = UIAlertAction(title: "Delete Photo", style: .destructive) { (action) in

}
deleteActionController.addAction(deleteAction)

present(deleteActionController, animated: true, completion: nil)
}
}

Answer

Your completion handler is not called since your alert has already been dismissed and you are again trying to dismiss the alert.

actionMainController.dismiss(animated: true) {

     self.showDeleteActionSheet()
}

Instead you should do some work on the completion handler of Action as

let deleteAction = UIAlertAction(title: "Delete Photo", style: .destructive) { (action) in

    self.showDeleteActionSheet() 
 }