【问题标题】:Dissmiss completion block doesn't work. Swift关闭完成块不起作用。迅速
【发布时间】:2016-11-11 11:02:44
【问题描述】:

我有PhotoBrowser(它继承自UIViewController)作为presentedViewController。我从UITableViewController 介绍它。当我点击按钮时,PhotoBrowser 呈现actionMainController(它继承自UIAlertController)。我想调用另一个UIAlertController,当我在actionMainController 中选择一个动作并在它被解散但完成块不起作用后立即显示第二个UIAlertController。我想补充一点,关闭actionMainController 成功,它可以工作

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)
    }
}

【问题讨论】:

  • 在那里放一个断点,它会被调用吗?
  • 从哪个类调用deletePhoto 方法?
  • @Tj3n 断点没有调用
  • @Anish웃 deletePhoto 正在被 PhotoBrowser 调用
  • 是在 PhotoBrowser 上定义的 deletePhoto() 方法吗?

标签: ios swift swift3 uialertcontroller completion-block


【解决方案1】:

您的完成处理程序未被调用,因为您的警报已被解除并且您再次尝试解除警报。

actionMainController.dismiss(animated: true) {

     self.showDeleteActionSheet()
}

相反,您应该在 Action 的完成处理程序上做一些工作

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

    self.showDeleteActionSheet() 
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-11
    相关资源
    最近更新 更多