gpt4 book ai didi

ios - 无法更改 View Controller 属性的实例

转载 作者:行者123 更新时间:2023-11-28 16:10:16 25 4
gpt4 key购买 nike

I have this button in my first view controller, when you press it, it triggers 2 action:

  • a function iterateItems
  • a segue which animates to ResultViewController

我需要将ResultViewControllerdataSource设置为bestComposition才能完成它的view,而这个属性的值只能获取iterateItems 完成后,我将此代码放入 iterateItems:

print(bestComposition.count)
let resultVC = ResultViewController()
resultVC.dataSource = bestComposition

但是,在ResultViewControllerviewDidAppear方法中,dataSource的值没有改变,这里是ResultViewController :

var dataSource = [WeaponItems]()
override func viewDidAppear(_ animated: Bool) {
print(dataSource.count)
...
}

dataSource的值在ViewController中是3,但是在ResultViewController中一直是0,为什么不改变呢?

编辑(准备segue的代码):

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let secondVC = segue.destination as! ResultViewController
secondVC.transitioningDelegate = self
secondVC.modalPresentationStyle = .custom
}

最佳答案

resultVC 是 ResultViewController 的一个实例,由这行新创建:

let resultVC = ResultViewController()

它不是作为 segue 的一部分自动创建的实例。 resultVC 永远不会显示,所以 viewDidAppear 永远不会运行,所以 print 语句永远不会执行。同时,呈现由 segue 创建的实例,因此它的 viewDidAppear 确实运行 - 但其 dataSource 的值未更改。尝试:

let resultVC = segue.destinationViewController as! ResultViewController

关于ios - 无法更改 View Controller 属性的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39656568/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com