Reputation: 188
Sup guys? Need some help over here. I used the delegate protocol to pass some strings back from a "second view controller" to it's previous one.
My array appends the string from the method I implemented on the delegate protocol, but everytime I press "add" and come back to the first screen, the table view doesn't change. I'm not sure if the string is not being passed between views or if it's just the table view that isn't reloading.
The code is quite simple:
import UIKit
class EsseVaiReceber: UIViewController, UITableViewDataSource, UITableViewDelegate, PassInfoDelegate {
@IBOutlet var listaDeCoisasAFazer: UITableView!
var arrayInfo : [String] = []
var stringReceived: String = ""
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
listaDeCoisasAFazer.reloadData()
print("SCREEN APPEARED")
}
override func viewDidLoad() {
super.viewDidLoad()
listaDeCoisasAFazer.reloadData()
print("LOADED SCREEN")
}
func passInfo(infothatwillpass: String) {
arrayInfo.append(infothatwillpass)
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrayInfo.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell!
let row = indexPath.row
let titulo = arrayInfo[row]
cell.textLabel!.text = titulo
return cell
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "add") {
let view = segue.destinationViewController as! EsseVaiPassar
view.delegate = self
}
}
}
and :
import Foundation
import UIKit
protocol PassInfoDelegate {
func passInfo(infothatwillpass: String)
}
class EsseVaiPassar: UIViewController {
@IBOutlet var campoTitulo: UITextField!
var delegate: PassInfoDelegate?
var textinput: String = ""
@IBAction func botaoAdicionarCoisasAFazer(sender: AnyObject) {
if campoTitulo == nil { return }
textinput = campoTitulo!.text!
print("TEXTO NO CAMPO:\(textinput)")
if delegate == nil {
print("DELEGATE IS NILL")
return
}
delegate!.passInfo(textinput)
print("TEXTO DO DELEGATE: \(textinput)")
if let navigation = self.navigationController {
navigation.popViewControllerAnimated(true)
print("INFO QUE VAI PASSAR: \(textinput)")
}
}
}
Any ideas why the table view won't populate? Thanks advanced =)
Upvotes: 0
Views: 49
Reputation: 919
The tableView is using arrayInfo
as the source for the listaDeCoisasAFazer
tableView data (indirectly). If you change arrayInfo
you need to tell listaDeCoisasAFazer
to reloadData()
.
func passInfo(infothatwillpass: String) {
arrayInfo.append(infothatwillpass)
}
You can/should remove the reloadData()
from viewWillLoad
and viewDidLoad
as the dataSource hasn't changed so there is no need to reload the data.
TIP
You are checking for nil
before the delegate
call in the destination viewController. Unless you are doing this for debugging purposes, you can simply call delegate?.passInfo(textinput)
. If delegate is nil, it will just ignore the call.
Upvotes: 1