How to Save Data and Segue Back to Collection View?

最后都变了- 提交于 2020-01-06 16:17:02

问题


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).

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.

Thanks in advance for the help!

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


回答1:


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.

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.




回答2:


I'm not sure I understand your question, but...

Use an unwind seque. See: What are Unwind segues for and how do you use them?

Implement an unwind segue action function in the CollectionViewController. Eg: @IBAction func saveNewTransation(segue: UIStoryboardSegue) { }

Make sure this unwind segue action gets called (eg, by the action of your Save button) in the NewTransactionViewController (control drag from the 'Save' button to the view controller's 'exit' symbol in interface builder and select the action from the popup that appears).

Then in your unwind segue, you will have access to the NewTransactionViewController that has requested the unwind seque via the UIStoryboardSegue's source property (a UIViewController). Once you have this NewTransactionViewController, you can access all of its properties (cast UIViewController to NewTransactionViewController first to make it easier).

Use these properties to add to your collection, and then refresh your view with the updated collection.



来源:https://stackoverflow.com/questions/40985792/how-to-save-data-and-segue-back-to-collection-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!