0

I have a UIView which allow user to save some data with their desirable name before it can be view in the 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)
}

the problems is when I ran the code above, it will come with this error: "while an existing transition or presentation is occurring; the navigation stack will not be updated." right before the alert will normally pop up then it crash and the alert doesn't work.

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

}

since the alert is needed before I can append any data to the class model. Any help would be appreciated

JameS
  • 231
  • 1
  • 2
  • 11
  • 1
    so the flow you want is VC1 -> ShowAlert -> click SaveAction -> dismiss alert and show nextVC? – mfaani Oct 27 '17 at 14:07
  • if that is what you want then you should place the `performsegue` call **inside** the `handler` of saveAction, so when save is clicked, the alert would be automatically dismissed and then it would show your nextVC – mfaani Oct 27 '17 at 15:13
  • @Honey yes, that exactly what I wanted. so, you mean I need IBAction instead of an outlet ? and put performsegue in there right ? – JameS Oct 27 '17 at 16:10

1 Answers1

1

You don't need another IBAction, you can benefit from the user tapping on the Save button. Right?

inside your Save's handler ie here:

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

add the performSegue(withIdentifier: "YOUR_SEGUE_IDENTIFIER", sender: self) right after print(saveCSV.lastPathComponent)

so once you click save, it will dismiss the alert and then perform that segue.

This would trigger your prepare(for segue...)

mfaani
  • 33,269
  • 19
  • 164
  • 293