嵌套的回调函数无法正常快速运行

时间:2018-11-24 10:12:48

标签: swift completionhandler

我正在尝试实现嵌套的完成处理程序,但是由于某种原因,我的第二个处理程序未在完成时触发。这就是代码的样子

//User presses a button on a cell. Code is in `cellForRowAtIndex...`
 cell.callback = {
            print("in CFRAIP")
            self.showPopUpDialog(completionHandler: { () -> Void in 
                print("AfterPOPUPDIALOG")
                self.requestBookingWithCompletionHandler(fetchBookingForDate: self.currentDate, row: indexPath.row)
            })
        }


func showPopUpDialog(completionHandler: () -> Void ){

    print("In show PopUPdialog")

    let alertController = UIAlertController(title: "Uppgifter", message: "Skriv in namn och telefonnummer", preferredStyle: .alert)

    let confirmAction = UIAlertAction(title: "Boka", style: .default) { (_) in

        //getting the input values from user
        self.bokadNamn = (alertController.textFields?[0].text)!
        self.bokadTelefon = (alertController.textFields?[1].text)!

        print("pressed ok in popup")
    }

    let cancelAction = UIAlertAction(title: "Avbryt", style: .cancel) { (_) in}

    //adding textfields to our dialog box
    alertController.addTextField { (textField) in
    textField.placeholder = "Namn"
    textField.layer.cornerRadius = 5
    }
    alertController.addTextField { (textField) in
    textField.placeholder = "Telefonnummer"
    }

    alertController.addAction(confirmAction)
    alertController.addAction(cancelAction)

    //finally presenting the dialog box
    self.present(alertController, animated: true, completion: nil)
}

这是我得到的照片:

in CFRAIP
In show PopUPdialog
pressed ok in popup
在用户按下print("AfterPOPUPDIALOG")中的ok之后,未触发

PopUP,并且我的网络请求未运行。我觉得这很简单,我很想念,但是不幸的是,我看不见它...

1 个答案:

答案 0 :(得分:2)

在函数func showPopUpDialog(completionHandler: () -> Void ) {中,您没有在任何地方调用完成处理程序completionHandler

如果您不调用完成处理程序,将如何执行打印语句print("AfterPOPUPDIALOG")

在适当的地方致电您的完成处理程序completionHandler