2016-12-06 99 views
0

tl; dr:我无法弄清楚如何从我的NewTransactionViewController返回到我的主CollectionViewController,并且在返回时,将NewTransactionViewController中的数据添加到我的集合列表视图中。我在网上找到的内容不是我所需要的就是太基本,就是太复杂。基本上我试图模仿一个UITableView,但我稍后使用更多动态功能的集合(加上我想要多列选项)。如何保存数据并返回集合视图?

我是新来的编程,正在尝试设置一个简单的预算应用程序来记录事务。目标是让用户通过填写​​详细信息并单击“保存”来添加新事务,然后返回到前一个VC并添加这些详细信息并更新收集视图。

在此先感谢您的帮助!

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

Storyboard Layout Example

回答

0

从你的故事板,它看起来像你有一个导航控制器和之中,该CollectionViewController是根视图控制器。然后将NewTransactionViewController压入导航控制器。

因此,从该NewTransactionViewController的一点,CollectionViewController是:

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

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

+0

感谢您的参考。有助于从该POV中思考它。 – naomijp

0

我不确定我是否理解你的问题,但...

使用展开序列。参见:What are Unwind segues for and how do you use them?

CollectionViewController中实现展开的继续动作函数。例如:@IBAction func saveNewTransation(segue: UIStoryboardSegue) { }

确保此开卷赛格瑞动作被调用(例如,通过您的Save按钮的动作)在NewTransactionViewController(调速拖动界面生成器中从“保存”按钮,在视图控制器的“退出”符号和从出现的弹出窗口中选择操作)。

然后在你的开卷SEGUE,你将有机会获得已通过UIStoryboardSegue'ssource财产(一UIViewController)要求开卷塞克的NewTransactionViewController。一旦你有了这个NewTransactionViewController,你可以访问它的所有属性(先铸UIViewControllerNewTransactionViewController以使其更容易)。

使用这些属性添加到您的集合,然后使用更新后的集合刷新您的视图。

+0

感谢您的帮助! – naomijp