如何在不使用 Storyboard segue 的情况下将项目从单独的 UIViewController 添加到 UICollectionViewController?

How to add item to a UICollectionViewController from a separate UIViewController without using Storyboard segue?

我有一个 UICollectionViewController 显示一组称为倒计时的数据。我有一个模态显示的 UIViewController,用户可以在其中创建新的倒计时。当用户点击 "Add"(一个 UIBarButtonItem)时,我想将 UIViewController 的新倒计时添加到 UICollectionViewController 上的数组(以及 collection 视图)。

我完全以编程方式构建我的应用程序(没有故事板文件)。我能找到的每个 article/stack 溢出 post/YouTube 教程都涉及使用 UIStoryBoardSegue and/or 是关于将数据从 CollectionView 推送到另一个 ViewController,而不是相反。我还没有找到一种 100% 以编程方式执行此操作的方法。

在我的搜寻过程中,我了解到纯粹以编程方式创建和使用 segue 通常是不好的。这是真的?如果是这样,我只需要使用委托吗?我将如何实施?

这是一些代码:

倒计时Class:(还有其他属性,但为了简单起见,我只包括标题属性)

class Countdown {

    var title: String!

    init?(title: String!) {

        // Fail initialization if string is blank
        if title.isEmpty {
            return nil
        }

        // Initializing property
        self.title = title

    }

}

CountdownCollectionViewController:(这是应用程序的主视图)

class CountdownCollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {

    var countdowns = [Countdown]()

    override func viewDidLoad() {
        super.viewDidLoad()

        loadSampleCountdowns()

    }

    private func loadSampleCountdowns() {

        // created 3 sample countdowns here

        countdowns += [countdown1, countdown2, countdown3]
    }

UIViewController:(从 CountdownsCollectionViewController 模态呈现)

class ViewController: UIViewController {

    var countdown: Countdown?

    // here I have a textfield property where the user enters a title for a new countdown 

    override func viewDidLoad() {
        super.viewDidLoad()


        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Add", style: .done, target: self, action: #selector(addTapped))

    }


    @objc func addTapped(_ sender: UIBarButtonItem) {

        let title = titleTextField.text ?? ""

        countdown = Countdown(title: title)

        // here is where I want an action to send the newly created countdown to CountdownCollectionViewController

}

我已经尝试在使用 segue 的 CountdownCollectionViewController 中使用这个函数,但是由于我没有故事板,所以它当然不起作用。但是,我认为我使用其中的代码朝着正确的方向前进:

func unwindToCountdownList(_ sender: UIStoryboardSegue) {

    if let sourceViewController = sender.source as? ViewController, let countdown = sourceViewController.countdown {

        // Add a new countdown:
        let newIndexPath = IndexPath(row: countdowns.count, section: 0)

        // Append the new countdown from UIViewController to the array:
        countdowns.append(countdown) 

        // Add new item to collectionView:
        collectionView.insertItems(at: [newIndexPath])
}

我会在这里使用委托。

创建 ViewControllerDelegate:

protocol ViewControllerDelegate: class {
    func didCreateCountDown(vc: ViewController, countDown: CountDown)
}

ViewController添加委托属性并在适当的时候调用它:

weak var delegate: ViewControllerDelegate?
@objc func addTapped(_ sender: UIBarButtonItem) {

    let title = titleTextField.text ?? ""

    countdown = Countdown(title: title)

    delegate.didCreateCountDown(vc: self, countDown: countDown)

    dismiss(animated: true, completion: nil)

}

使CountdownCollectionViewController符合ViewControllerDelegate:

extension CountdownCollectionViewController : ViewControllerDelegate {
    func didCreateCountDown(vc: ViewController, countDown: CountDown) {
        // Add a new countdown:
        let newIndexPath = IndexPath(row: countdowns.count, section: 0)

        // Append the new countdown from UIViewController to the array:
        countdowns.append(countdown) 

        // Add new item to collectionView:
        collectionView.insertItems(at: [newIndexPath])
    }
}

虽然你没有显示这个,但是CountdownCollectionViewController中应该有一个地方你调用present来显示ViewController:

let vc = ViewController()
...
present(vc, animated: true, completion: nil)

就在present之前,您可以将self设置为代表:

vc.delegate = self