【问题标题】:Array.append is overwriting not appendingArray.append 覆盖不附加
【发布时间】:2020-05-19 22:00:38
【问题描述】:

我有两个视图控制器,一个包含一个包含两个部分的表格视图,其中显示电影列表,另一个视图控制器可以添加电影。假设包含表视图的视图控制器是 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)
    }

【问题讨论】:

  • 如果您真的认为 Array.append() 不起作用,请尝试一个接一个地附加两个内容。你会发现它运行良好。

标签: ios arrays swift uitableview


【解决方案1】:

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

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

【讨论】:

  • 我该怎么做?
  • 您之前是否通过创建协议使用过委托?
  • 如果不是 .. 然后分享你推送控制器的类名和方法
  • 不,我实际上没有使用协议,我会更新问题
  • 完成!我写了类名
猜你喜欢
  • 1970-01-01
  • 2020-07-01
  • 2016-04-04
  • 1970-01-01
  • 1970-01-01
  • 2011-12-17
  • 2012-06-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多