我有两个视图控制器,一个包含一个包含两个部分的表格视图,在其中显示影片列表,另一个包含可以添加电影的视图控制器。假定包含表视图的视图控制器为VC1,添加影片为VC2。
问题是当我在VC2中添加电影并将其附加到VC1中的电影列表时,它只是替换了先前添加的电影。
我仅在运行时在本地添加电影。因此,每当我运行该应用程序时,列表最初包含0部电影。因此,我添加了电影,然后将其添加到列表中。当我尝试添加另一部电影时,它将替换之前的电影,好像列表中不能包含多个电影。

这是我追加数组的地方:

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

但是当我回到VC1时,我发现数据已替换而不是附加。有什么办法吗?

编辑:
包含我显示的数据的tableView的第一个视图控制器称为ViewController。添加影片的第二个影片称为AddMovie。

这就是我导航到AddMovie的方式:
@IBAction func addMovieButton(_ sender: UIBarButtonItem) {
        let vc = self.storyboard?.instantiateViewController(withIdentifier: "AddMovie") as! AddMovie
        self.navigationController?.pushViewController(vc, animated: true)
    }

最佳答案

您每次都在创建新的实例...

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

您需要获取当前内容,然后追加。要附加电影,您可以使用委托以及闭包

使用委托
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)
        }
    }

}

将这样的扩展名写入viewController
extension ViewController: AddMoviesToController {
    func addMoviesToController(movie:Movie , image:UIImageView) {
        //add movies here
        customMovies.append(movie)
        customImages.append(image)
       tableView.reloadData()
    }
}

在推动控制器的同时
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)
    }
}

关于ios - Array.append被覆盖而不是附加,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61901488/

10-14 04:55