如何保存数据并转回集合视图? [英] How to Save Data and Segue Back to Collection View?

查看:21
本文介绍了如何保存数据并转回集合视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

tl;dr:我在弄清楚如何从我的 NewTransactionViewController 转回到我的主 CollectionViewController 以及返回时,将来自 NewTransactionViewController 的数据添加到我的集合列表视图中时遇到了麻烦.我在网上找到的东西要么对我需要的东西来说太基本,要么太复杂.本质上,我正在尝试模仿 UITableView,但稍后我将使用集合来获得更多动态功能(另外我想要多列的选项).

tl;dr: I'm having trouble figuring out how to segue from my NewTransactionViewController back to my main CollectionViewController and when going back, adding the data from the NewTransactionViewController into my collection list view. What I've found online is either too basic for what I need or too sophisticated. Essentially I'm trying to mimic a UITableView but I'm using collections for more dynamic capabilities later on (plus I want the option of multiple columns).

我是编程新手,正在尝试设置一个简单的预算应用程序来记录交易.目标是让用户通过填写​​详细信息来添加新交易,当他们点击保存"时,它会返回到之前的 VC 并添加这些详细信息并更新集合视图.

I'm new to programming and am trying to set-up a simple budget app to log transactions. Goal is for the user to add a new transaction by filling in details and when they click "save", it goes back to the previous VC and adds those details and updates the collection view.

预先感谢您的帮助!

//  ViewController.swift
import UIKit

class NewTransactionViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate
{
    //Add new transaction cell to master list
    @IBAction func addNewTransaction(_ sender: UIBarButtonItem)
    {
        //Unsure how to implement
    }

class CollectionViewController: UICollectionViewController
{
    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
    {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as UICollectionViewCell

        let categoryLabel = cell.viewWithTag(1) as! UILabel
        categoryLabel.text = categoryArray[indexPath.row]

        let balanceLabel = cell.viewWithTag(2) as! UILabel
        balanceLabel.text = balanceArray[indexPath.row]

        return cell
    } 
}

故事板布局示例

推荐答案

从您的故事板来看,您似乎有一个导航控制器,其中的 CollectionViewController 是根视图控制器.然后将 NewTransactionViewController 推送到导航控制器上.

From your storyboard, it looks like you have a navigation controller and within that, the CollectionViewController is the root view controller. And then the NewTransactionViewController is pushed onto the navigation controller.

所以,从 NewTransactionViewController 的角度来看,CollectionViewController 是:

So, from the point of view of the NewTransactionViewController, the CollectionViewController is:

self.navigationController!.viewControllers[0] as! CollectionViewController

通过该引用,您现在可以调用方法或设置集合视图控制器的属性.

With that reference, you can now call a method or set a property of the collection view controller.

这篇关于如何保存数据并转回集合视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆