0

I'm trying to append to an array via segue:

@IBOutlet weak var addTextTextField: UITextField!    
@IBAction func addTextButton(_ sender: UIButton)
    {
        performSegue(withIdentifier: "addTextSegue", sender: self)
    }    
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
    {
        let mainViewController = segue.destination as! MainViewController
        mainViewController.array.append(addTextTextField.text!)
    }    

Its only appending to the array once, the next time I try, it overwrites the last entry. What is the reason for this?

PerNil
  • 187
  • 1
  • 11
  • Every segue (except an unwind segue) creates a new destination viewcontroller. What is the control flow of your application? Does MainViewController present this VC (the one with addTextButton) in some way? – vacawama Feb 08 '18 at 17:36
  • Yes, in MainViewController I have a button that presents (segues) a second View Controller where the `addTextTextField` and the `addTextButton` is. – PerNil Feb 08 '18 at 18:02
  • OK. Use an *unwind segue* to return to the `MainViewController` like I did in [this answer](https://stackoverflow.com/a/30992088/1630618). – vacawama Feb 08 '18 at 18:15
  • @vacawama I´m not able to control drag to the exit icon. Nothing happens when trying to control drag. The exit icon doesn't respond. Any ideas? – PerNil Feb 09 '18 at 06:56
  • Are you control dragging from a button or the ViewController icon? – vacawama Feb 09 '18 at 12:31
  • I have tried both from the button, and the ViewController icon. – PerNil Feb 09 '18 at 19:40
  • I'm sorry, it was a Xcode bug. Needed to restart computer. – PerNil Feb 09 '18 at 19:56
  • @vacawama Last question: Now the segue works, but it adds two entries to the array. If I type in "Test" in the text field, the array reads: Test, Test. What could be the reason for this? – PerNil Feb 09 '18 at 20:58
  • Uh, did you wire it from a button and then also call it programmatically? You'll get 2 segues that way. – vacawama Feb 09 '18 at 21:12
  • Yes, that was it... When using unwind segue, is it possible to also use a "normal" segue from another button? I would like a cancel button that takes the user back to the previous VC without adding to the array? – PerNil Feb 10 '18 at 09:50
  • You should just use a second unwind segue with a different id. A regular segue will create a new VC which you don't want. – vacawama Feb 10 '18 at 12:05

0 Answers0