尝试unwindsegue时出错UIAlert

时间:2017-10-27 12:42:37

标签: ios iphone swift segue uialertview

我有一个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)
}

问题是当我运行上面的代码时,会出现这个错误:"当现有的转换或演示正在发生时;导航堆栈不会更新。" 在警报正常弹出之前,然后崩溃,警报无法正常工作。

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

}

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

1 个答案:

答案 0 :(得分:1)

您不需要其他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)
    }))

performSegue(withIdentifier: "YOUR_SEGUE_IDENTIFIER", sender: self)

之后添加print(saveCSV.lastPathComponent)

因此,一旦您单击“保存”,它将关闭警报,然后执行该segue。

这会触发你的准备(对于segue ......)