嵌套回调函数无法正常工作 swift
Nested callback function not working properly swift
我正在尝试实现嵌套的完成处理程序,但出于某种原因,我的第二个处理程序没有在完成时触发。这是代码的样子
//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")
没有被触发,并且在用户在 PopUP
中按下确定后我的网络请求没有被 运行。我觉得这是我想念的非常简单的东西,但不幸的是,我看不到它...
在函数 func showPopUpDialog(completionHandler: () -> Void ) {
内,您没有在任何地方调用完成处理程序 completionHandler
。
如果您不调用完成处理程序,打印语句 print("AfterPOPUPDIALOG")
将如何执行?
在适当的地方调用完成处理程序 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")
没有被触发,并且在用户在 PopUP
中按下确定后我的网络请求没有被 运行。我觉得这是我想念的非常简单的东西,但不幸的是,我看不到它...
在函数 func showPopUpDialog(completionHandler: () -> Void ) {
内,您没有在任何地方调用完成处理程序 completionHandler
。
如果您不调用完成处理程序,打印语句 print("AfterPOPUPDIALOG")
将如何执行?
在适当的地方调用完成处理程序 completionHandler
。