1

I have an array in my ViewOne, which I pass to ViewTwo with the prepareForSegue function.

When I delete data of my array in ViewTwo and go back with the back button on the NavigationController or by swiping to right, the array in ViewOne has still all the data and don't know that I deleted something of it.

Is there any solution for this problem?

ViewOne:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if(segue.identifier == "submitSegue") {
            let shoppingCart = segue.destinationViewController as! ShoppinCartScreen;
            if(artikel != nil) {
                shoppingCart.alleArtikel = alleArtikel
                print("Test: \(artikel?.artikelnummer)")
            }
        }
    }

ViewTwo:

var alleArtikel = [Artikel]()

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return alleArtikel.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("ShoppingCartScreenCell", forIndexPath: indexPath) as!ShoppingCartScreenCell
        let eintrag = alleArtikel[indexPath.row]
        cell.previewImage.image = eintrag.foto
        cell.artikelNummer.text = eintrag.artikelnummer
        return cell
    }

    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if(editingStyle == .Delete) {
            alleArtikel.removeAtIndex(indexPath.row)
            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
        }
    }

I have a main View (ViewOne) and a TableView(ViewTwo).

rmaddy
  • 314,917
  • 42
  • 532
  • 579
user3653164
  • 979
  • 1
  • 8
  • 24
  • First you really need to show some code. Are you using a tableview? How are you saving the data in the array on ViewTwo? If you are using a tableview are you performing a tableview refresh? – MwcsMac Dec 14 '15 at 18:34
  • In case ViewOne is a tableview - you should refresh it or you can read about Unwind segues. – Oleshko Dec 14 '15 at 18:38
  • I have added some code, hope it will help you to help me. In my ViewTwo I am using a TableView. No I'm not performing a tableview refresh. – user3653164 Dec 14 '15 at 18:39
  • Not really sure what you are trying to do, but take a look at [this answer](http://stackoverflow.com/a/32590635) that I posted a while back, it might get you started. – deadbeef Dec 14 '15 at 18:46

2 Answers2

0

In Swift, arrays are value types not reference types (see Apple Developer Blog entry), so when you 'pass' it to view2 you are really just making a copy. View2 modifies the copy but the original remains unchanged.

Ali Beadle
  • 4,486
  • 3
  • 30
  • 55
  • Is there any reference-type array in swift? – user3653164 Dec 14 '15 at 18:49
  • @user3653164 objects are reference types - so you could encapsulate your data in a class, and pass a reference to that - but there are other ways, it depends on what you wish to achieve. – Ali Beadle Dec 14 '15 at 18:51
  • @user3653164 In general I try to avoid storing data in view controllers. Store data in other objects and pass references to those objects between your view controllers as necessary. – Ali Beadle Dec 14 '15 at 18:57
  • Could you give me an example of it? I was thinking of a little class with only an array inside, but is it possible to access the object of this class in both ViewControllers? – user3653164 Dec 14 '15 at 18:59
  • @user3653164 Yes, at a simple level you could create an object of this class in view1 and then pass the reference to it to view2. However if this is destined to be anything other than a simple app you may want to take a step back and consider how you are going to organise all your data and views to work together. Are you aware of the Model-View-Controller design pattern for this kind of thing - [MVC](https://developer.apple.com/library/ios/documentation/General/Conceptual/DevPedia-CocoaCore/MVC.html) – Ali Beadle Dec 14 '15 at 19:07
0

Instead of using a prepareForSegue method when going back, which to my knowledge is not possible, just run the viewWillDisappear function. Basically this is the viewDidLoad function for when the view disappears. It will run when you are performing a segue when you are leaving the view.

example:

override func viewWillDisappear(animated: Bool)
{
    super.viewWillDisappear(animated)
    print("the view is hidden")
}
Stefan DeClerck
  • 1,162
  • 2
  • 12
  • 22
  • But how can I change the array in ViewOne when I use viewWillDisappear in ViewTwo? I want to make something like this `ViewOne.array = ViewTwo.array` after I'm going back from ViewTwo to ViewOne. – user3653164 Dec 14 '15 at 18:58