我一直在尝试将一些数据传递给模型文件,以便在用户选中保存按钮后使用弹出警报在另一个视图控制器中使用,它会触发准备功能并在另一个视图控制器上展开到tableview
@IBAction func saveButton(_ sender: UIBarButtonItem) {
saveFilesName()
}
然后,
func saveFilesName() {
let alert = UIAlertController(title: "Save As", message: "Please enter the file name", preferredStyle: .alert)
alert.addTextField(configurationHandler: {(nameField) -> Void in
nameField.placeholder = "Enter the file name"
nameField.textAlignment = .center
})
alert.addTextField(configurationHandler: {(descField) -> Void in
descField.placeholder = "Enter the your description"
descField.textAlignment = .center
})
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (actions: UIAlertAction) -> Void in
}))
alert.addAction(UIAlertAction(title: "Save", style: .default, handler: { (actions: UIAlertAction) -> Void in
let nameFieldData = alert.textFields![0]
let descFieldData = alert.textFields![1]
self.fileName = nameFieldData.text ?? ""
self.descData = descFieldData.text ?? ""
print(alert.textFields![0])
let saveCSV = self.saveCSVFiles()
print(saveCSV.lastPathComponent)
self.performSegue(withIdentifier: "unwindToCSVList", sender: self)
}))
self.present(alert, animated: true, completion: nil)
}
但是在选中保存按钮后,它会按预期展开到表视图,但是prepare方法总是返回没有按下保存按钮,取消这意味着准备方法没有正确触发
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
super.prepare(for: segue, sender: sender)
if segue.identifier == "unwindToCSVList" {
guard let button = sender as? UIBarButtonItem, button === saveButton else {
os_log("The save button was not pressed, cancelling", log: OSLog.default, type: .debug)
return
}
let nsurl = String(describing: csvFile)
let name = fileName ?? ""
let description = descData ?? ""
let photo = UIImage(contentsOfFile: "defaultImage")
let url = NSURL(fileURLWithPath: nsurl)
print("yeah")
csv = CSVData(name: name, description: description, photo: photo, url: url)
}
}
任何建议都将不胜感激