代码之家  ›  专栏  ›  技术社区  ›  JameS

尝试执行unwindsegue时出错UIAlert

  •  0
  • JameS  · 技术社区  · 7 年前

    我有一个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 回复  |  直到 7 年前
        1
  •  1
  •   mfaani    7 年前

    你不需要另一个IBAction,你可以从用户点击保存按钮中受益。正当

    在…内 你的 Save 的处理程序ie:

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