Ios 保存的对象数组未显示在新的ViewController中
每次按下“添加”按钮时,我都试图将该特定项附加到项数组中。它在控制台中为每个新单元格打印,但是当我推到一个新的ViewController(它将是所有添加项的摘要)时,它不会打印这些项。只打印空数组Ios 保存的对象数组未显示在新的ViewController中,ios,swift,Ios,Swift,每次按下“添加”按钮时,我都试图将该特定项附加到项数组中。它在控制台中为每个新单元格打印,但是当我推到一个新的ViewController(它将是所有添加项的摘要)时,它不会打印这些项。只打印空数组 class CollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout, PostCellDelegate { var finalList = [Item]()
class CollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout, PostCellDelegate {
var finalList = [Item]()
@objc func addTapped(cell: PostCell) {
guard let indexPath = self.collectionView.indexPath(for: cell) else {return}
hiddenRows.insert(indexPath.row)
cell.removeButton.isHidden = false
let item = itemsArr[indexPath.row]
finalList.append(item)
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! PostCell
cell.delegate = self
let item = itemsArr[indexPath.row]
cell.set(name: item.name, brand: item.brand, price: item.price)
print(finalList)
return cell
}
@objc private func handleNext() {
let nextIndex = min(pageControl.currentPage + 1, itemsArr.count - 1)
let indexPath = IndexPath(item: nextIndex, section: 0)
if pageControl.currentPage == 4{
let checkoutView = FinishViewController()
self.navigationController?.pushViewController(checkoutView, animated: true)
checkoutView.modalPresentationStyle = .overCurrentContext
present(checkoutView, animated: true)
print("last item")
}else {
print("not last")
}
pageControl.currentPage = nextIndex
collectionView?.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
}
lazy var pageControl: UIPageControl = {
let pc = UIPageControl()
pc.currentPage = 0
pc.numberOfPages = 4
pc.currentPageIndicatorTintColor = .red
pc.pageIndicatorTintColor = UIColor(red: 249/255, green: 207/255, blue: 224/255, alpha: 1)
return pc
}()
class FinishViewController: UIViewController {
let cV = CollectionViewController()
override func viewDidLoad() {
print(cV.finalList)
super.viewDidLoad()
view.backgroundColor = .red
}
@objc func addTapped(单元格:后单元格){
guard let indexPath=self.collectionView.indexPath(for:cell)else
{return}
insert(indexPath.row)
cell.removeButton.ishiden=false
让item=itemsArr[indexPath.row]
finalList.append(项目)
collectionView?.reloadData()@objc func addTapped(单元格:PostCell){
guard let indexPath=self.collectionView.indexPath(for:cell)else
{return}
insert(indexPath.row)
cell.removeButton.ishiden=false
让item=itemsArr[indexPath.row]
finalList.append(项目)
collectionView?.reloadData()问题在于,在FinishViewController
中,您正在初始化CollectionViewController
的新实例,因此该属性具有默认的空数组值
当您呈现或切换到该数组时,需要将该数组传递到FinishViewController
在FinishViewController
中添加以下内容:
var finalList = [Item]()
在handleNext
中,确保正确设置finalList
:
let checkoutView = FinishViewController()
checkoutView.finalList = self.finalList
问题在于,在FinishViewController
中,您正在初始化CollectionViewController
的新实例,因此该属性具有默认的空数组值
当您呈现或切换到该数组时,需要将该数组传递到FinishViewController
在FinishViewController
中添加以下内容:
var finalList = [Item]()
在handleNext
中,确保正确设置finalList
:
let checkoutView = FinishViewController()
checkoutView.finalList = self.finalList
我添加了一行,但是在显示新视图时它仍然打印空数组。我添加了一行,但是在显示新视图时它仍然打印空数组。