How to feed a tableView of a screen with information from another screen?

0

Good evening, I have 3 screens in my app to buy passwords, the last one has a tableView powered by the passwords purchased by the user on a previous screen. He must choose a password and it appears in the summary of his request on the next screen in a tableView, if the user wants to buy another password, he can click "next password" and return to the screen of passwords and choose another to be added the one that was chosen previously, but how do you get the information added after choosing the new passwords?

The flow looks like this:

Categories screen: The user chooses which category and goes to the password screen Password screen: The user chooses the password and goes to the summary screen Summary screen: The tableView is fed with the chosen password and if the user wants to buy another, return to the categories screen.

Class where passwords are chosen:

import UIKit

class ComprarSenhaViewController {

     static let sharedInstance = ComprarSenhaViewController()

     var senhasSelecionadas = Array<String>()
     var senhaAtual = Senha()

     @IBAction func fecharPedido(sender: AnyObject) {
        SenhaController.sharedInstance.senha = self.senhaAtual

        senhasSelecionadas.append("\(senhaAtual.id)")

        let storyBoard = UIStoryboard(name: "Main", bundle: nil)
        let novoNavigation = storyBoard.instantiateViewControllerWithIdentifier("ResumoViewController")
        self.navigationController?.pushViewController(novoNavigation, animated: true)
    }
}

Class where the summary of the chosen passwords is shown:

import UIKit

class ResumoViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{

    static let sharedInstance = ResumoViewController()

    var senhasSelecionadas = Array<String>()

    @IBOutlet weak var tableView: UITableView!

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return senhasSelecionadas.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let row = indexPath.row
        let idSenha = senhasSelecionadas[row]
        let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: nil)
        cell.textLabel!.text = idSenha
        return cell
    }

    func carregarResumoSenhas(){

        ComprarSenhaViewController.sharedInstance.senhasSelecionadas = self.senhasSelecionadas
        let idsenha = SenhaController.sharedInstance.senha.id
        senhasSelecionadas.append("\(idsenha)")

        tableView.reloadData()

    }
    @IBAction func proximaSenha(sender: AnyObject) {        
        let storyBoard = UIStoryboard(name: "Main", bundle: nil)
        let novoNavigation = storyBoard.instantiateViewControllerWithIdentifier("CategoriaViewController")
        self.navigationController?.pushViewController(novoNavigation, animated: true)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        carregarResumoSenhas()

    }
    
asked by anonymous 13.11.2015 / 03:08

2 answers

0

Do not use Delegate because you will be creating an unnecessary dependency. senhasSelecionadas is a public variable and ComprarSenhaViewController has a reference to ResumoViewController , so just assign a value to senhasSelecionadas just before pushViewController .

...
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let resumoController = storyBoard.instantiateViewControllerWithIdentifier("ResumoViewController")
resumoController.senhasSelecionadas = senhasSelecionadas
self.navigationController?.pushViewController(resumoController, animated: true)
...

I also do not understand why you are using Singleton in your ViewControllers .

    
13.11.2015 / 18:08
3

Using protocol , as mentioned, you can do this as follows: the protocol definition will be in its ResumoViewController class, this will be called ResumoDelegate .

In this protocol I will define a method that will return all purchased passwords, and thus in viewWillAppear search that list and update the summary table.

protocol ResumoDelegate {
    func listaSenhas() -> [Senha]
}

class ResumoViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    var delegate: ResumoDelegate?
    var senhasSelecionadas: [Senha] = []

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)

        senhasSelecionadas = delegate?.listaSenhas()

        tableView.reloadData()
    }
}

Now the implementation of this method is on your ComprarSenhaViewController screen:

class ComprarSenhaViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, ResumoDelegate {
    var senhasSelecionadas: [Senha] = []

    @IBAction func fecharPedido(sender: AnyObject) {
        senhasSelecionadas.append(senhaAtual)

        let storyBoard = UIStoryboard(name: "Main", bundle: nil)
        let resumoController = storyBoard.instantiateViewControllerWithIdentifier("ResumoViewController")
        resumoController.delegate = self

        self.navigationController?.pushViewController(resumoController, animated: true)
    }

    func listaSenhas() -> [Senha] {
        return senhasSelecionadas
    }
}

Note that you also need to include ResumoDelegate in the class signature and before pushViewController set delegate to the class itself.

    
13.11.2015 / 11:47