0
Good evening, I have 3 screens in my app to buy passwords, the last of them has a Windows tablet powered by the passwords purchased by the user on a previous screen. He must choose a password and it should appear in his order summary on the next screen on a Windows tableView, if the user wants to buy another password, he can click on "next password" and return to the password screen and choose another to be added to the one previously chosen, but how to get the information added after choosing new passwords?
The flow goes like this:
Categories screen: User chooses which category and goes to password screen Password screen: User chooses password and goes to summary screen Summary screen: The tableView is fed with the chosen password and if the user wants to buy another one, back 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 showing summary of chosen passwords:
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()
}
You are wearing delegate? Delegation is a design Pattern that Enables a class or Structure to hand off (or delegate) some of its responsibilities to an instance of Another type.
– Marcelo Bonus