如何保存数据并转回集合视图?
How to Save Data and Segue Back to Collection View?
tl;dr:我无法弄清楚如何从我的 NewTransactionViewController 返回到我的主 CollectionViewController 以及返回时,将 NewTransactionViewController 中的数据添加到我的集合列表视图中。我在网上找到的东西对于我需要的东西来说要么太基础,要么太复杂。本质上,我正在尝试模仿 UITableView,但稍后我将使用集合来获得更多动态功能(另外我想要多列的选项)。
我是编程新手,正在尝试设置一个简单的预算应用程序来记录交易。目标是让用户通过填写详细信息添加新交易,当他们单击 "save" 时,它会返回到之前的 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
从您的情节提要来看,您似乎有一个导航控制器,在其中,CollectionViewController 是根视图控制器。然后将 NewTransactionViewController 推送到导航控制器上。
所以,从NewTransactionViewController的角度来看,CollectionViewController是:
self.navigationController!.viewControllers[0] as! CollectionViewController
使用该引用,您现在可以调用方法或设置集合视图控制器的 属性。
我不确定我是否理解你的问题,但是...
使用展开序列。参见:What are Unwind segues for and how do you use them?
在 CollectionViewController
中实现一个 unwind segue 动作函数。例如:@IBAction func saveNewTransation(segue: UIStoryboardSegue) {
}
确保在 NewTransactionViewController
(控制从 'Save' 按钮拖动到视图控制器的'exit' 界面生成器中的符号和 select 出现的弹出窗口中的操作)。
然后在您的展开序列中,您将可以通过 UIStoryboardSegue's
source
属性 访问已请求展开序列的 NewTransactionViewController
(UIViewController
).拥有此 NewTransactionViewController
后,您可以访问其所有属性(首先将 UIViewController
转换为 NewTransactionViewController
以使其更容易)。
使用这些属性添加到您的 collection,然后使用更新后的 collection.
刷新您的视图
tl;dr:我无法弄清楚如何从我的 NewTransactionViewController 返回到我的主 CollectionViewController 以及返回时,将 NewTransactionViewController 中的数据添加到我的集合列表视图中。我在网上找到的东西对于我需要的东西来说要么太基础,要么太复杂。本质上,我正在尝试模仿 UITableView,但稍后我将使用集合来获得更多动态功能(另外我想要多列的选项)。
我是编程新手,正在尝试设置一个简单的预算应用程序来记录交易。目标是让用户通过填写详细信息添加新交易,当他们单击 "save" 时,它会返回到之前的 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
从您的情节提要来看,您似乎有一个导航控制器,在其中,CollectionViewController 是根视图控制器。然后将 NewTransactionViewController 推送到导航控制器上。
所以,从NewTransactionViewController的角度来看,CollectionViewController是:
self.navigationController!.viewControllers[0] as! CollectionViewController
使用该引用,您现在可以调用方法或设置集合视图控制器的 属性。
我不确定我是否理解你的问题,但是...
使用展开序列。参见:What are Unwind segues for and how do you use them?
在 CollectionViewController
中实现一个 unwind segue 动作函数。例如:@IBAction func saveNewTransation(segue: UIStoryboardSegue) {
}
确保在 NewTransactionViewController
(控制从 'Save' 按钮拖动到视图控制器的'exit' 界面生成器中的符号和 select 出现的弹出窗口中的操作)。
然后在您的展开序列中,您将可以通过 UIStoryboardSegue's
source
属性 访问已请求展开序列的 NewTransactionViewController
(UIViewController
).拥有此 NewTransactionViewController
后,您可以访问其所有属性(首先将 UIViewController
转换为 NewTransactionViewController
以使其更容易)。
使用这些属性添加到您的 collection,然后使用更新后的 collection.
刷新您的视图