ios - 尝试展开 segue 时出现错误 UIAlert

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

}

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

最佳答案

您不需要另一个 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...)

关于ios - 尝试展开 segue 时出现错误 UIAlert,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46975331/

相关文章:

iOS - 是美国的用户吗?

iphone - ABPeoplePickerNavigationController 仅在模拟器中被解雇时崩溃

iphone - 渲染 2d 动画 Sprite openGl ES 的最佳方式

iphone - 切换 View 后 UIImageView 在屏幕上 float

swift - 将捏合手势方法的缩放限制为屏幕的高度

iOS - 核心数据 - 创建一个空的 NSManagedObjectContext

ios - 如何在 iOS 应用程序中显示最近查看的项目(例如 eBay)

ios - 如何使用 FLAnimatedImage 检查动画的结束

ios - Swift 反射 - 如何检查反射值是否是一种类型

swift - 将字符串格式化为帕斯卡大小写