尝试执行 unwindsegue 时出现错误 UIAlert

Error UIAlert when trying to do unwindsegue

我有一个 UIView,它允许用户在可以在 tableView 中查看之前使用他们想要的名称保存一些数据

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.present(alert, animated: true, completion: nil)
}

问题是当我 运行 上面的代码时,它会出现这个错误: "while an existing transition or presentation is occurring; the navigation stack will not be updated." 就在警报通常会弹出之前它崩溃并且警报不起作用。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    super.prepare(for: segue, sender: sender)

    guard let button = sender as? UIBarButtonItem, button === saveButton else {
        os_log("The save button was not pressed, cancelling", log: OSLog.default, type: .debug)
        return
    }

    saveFilesName()

    if fileName != nil {
    let nsurl = String(describing: csvFile)

    let name = fileName
    let description = descData
    let photo = UIImage(contentsOfFile: "defaultImage")
    let url = NSURL(fileURLWithPath: nsurl)

    csv = CSVData(name: name!, description: description!, photo: photo, url: url)
    }

}

因为在我可以将任何数据附加到 class 模型之前需要警报。任何帮助将不胜感激

您不需要另一个 IBAction,您可以从用户点击保存按钮中获益。对吗?

里面 你的 Save 的处理程序,即这里:

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

print(saveCSV.lastPathComponent)

之后添加 performSegue(withIdentifier: "YOUR_SEGUE_IDENTIFIER", sender: self)

所以一旦您点击保存,它就会解除警报,然后执行该 segue。

这将触发您的准备(对于 segue...)