Mostafa Hendawi
Mostafa Hendawi

Reputation: 76

Array.append is overwriting not appending

I have two view controllers, one contains a table view with two sections where I display a list of movies, and another view controller where I can add movies. Assume the view controller that contains the table view is VC1 and the add movie is VC2. The problem is when I add a movie in VC2 and append it to the list of movies in VC1, it just replaces the previously added movie. I only add the movies locally during runtime. So whenever I run the app the list initially contains 0 movies. So I add a movies and then append it to the list. When I try to add another movie, it replaces the one before it as if the list cannot contain more than one item.

This is where I append the array:

@IBAction func addMovieButtonClicked(_ sender: UIButton) {
        if imageView.image == nil || movieTitleField.text == "" || movieDateField.text == "" || movieOverviewField.text == "" {
            displayAlert(title: "Warning", message: "Please enter all data to be able to add your movie!")
        } else {
            let vc = self.storyboard?.instantiateViewController(withIdentifier: "ViewController") as! ViewController
            let movie = Movie(title: movieTitleField.text!, overview: movieOverviewField.text!, date: movieDateField.text!)
            vc.customMovies.append(movie)
            vc.customImages.append(imageView)
            self.navigationController?.pushViewController(vc, animated: true)
        }
    }

but when I move back to VC1 I find the data is replaced and not appended. Any solutions?

Edit: The first view controller that contains the tableView that I show the data is called ViewController. The second one where I add a movie is called AddMovie.

This is how I navigate to the AddMovie:

@IBAction func addMovieButton(_ sender: UIBarButtonItem) {
        let vc = self.storyboard?.instantiateViewController(withIdentifier: "AddMovie") as! AddMovie
        self.navigationController?.pushViewController(vc, animated: true)
    }

Upvotes: 1

Views: 87

Answers (1)

Jawad Ali
Jawad Ali

Reputation: 14397

You are creating new instence every time ...

 self.storyboard?.instantiateViewController(withIdentifier: "ViewController") as! ViewController

You need to get the current and then append. To append movies you can use delegates as well as Closures

To use Delegate

protocol AddMoviesToController:AnyObject {
    func addMoviesToController(movie:Movie , image:UIImage)
}

class AddMovie:UIViewController {
    weak var delegate:AddMoviesToController?


    @IBAction func addMovieButtonClicked(_ sender: UIButton) {
        if imageView.image == nil || movieTitleField.text == "" || movieDateField.text == "" || movieOverviewField.text == "" {
            displayAlert(title: "Warning", message: "Please enter all data to be able to add your movie!")
        } else {
            let vc = self.storyboard?.instantiateViewController(withIdentifier: "ViewController") as! ViewController
            let movie = Movie(title: movieTitleField.text!, overview: movieOverviewField.text!, date: movieDateField.text!)
            delegate?.addMoviesToController(movie: movie, image: imageView)
            self.navigationController?.pushViewController(vc, animated: true)
        }
    }

}

write extension like this to viewController

extension ViewController: AddMoviesToController {
    func addMoviesToController(movie:Movie , image:UIImageView) {
        //add movies here
        customMovies.append(movie)
        customImages.append(image)
       tableView.reloadData()
    }
}

And while pushing your controller

class ViewController: UIViewController {
    @IBAction func addMovieButton(_ sender: UIBarButtonItem) {
        let vc = self.storyboard?.instantiateViewController(withIdentifier: "AddMovie") as! AddMovie
        vc.delegate = self
        self.navigationController?.pushViewController(vc, animated: true)
    }
}

Upvotes: 3

Related Questions