I have a vc passing an array to the second vc. This second vc changes the array and must pass the modified array to the first vc when dismissed.
Pass the array the first to the second, I managed
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc : MedicacaoPopUp = storyboard.instantiateViewControllerWithIdentifier("MedicacaoPopUp") as! MedicacaoPopUp
vc.nomeAlunoP = nomeAluno.text!
vc.turmaAlunoP = turmaAluno.text!
vc.medicacao = medicacao
vc.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext
self.presentViewController(vc, animated: true, completion: nil)
Pass the modified array back to the first vc could not
a) I created the protocol inside the first vc file, I deletegated and I created the func to chance the value of the array
protocol MedicacaoAddDelegate: class {
func receiveRemedios(rMedicacao: [AnyObject])
}
class MedicacaoAdd: UIViewController, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate, MedicacaoAddDelegate {
var medicacao = [AnyObject]()
func receiveRemedios(rMedicacao: [AnyObject]) {
print("dentro do receiveRemedios")
medicacao = rMedicacao
trataCesto()
}
b) In the second vc I created the variable "delegate" and called the function when a button is pressed
class MedicacaoPopUp: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableview: UITableView!
weak var delegate: MedicacaoAddDelegate?
var nomeAlunoP = ""
var turmaAlunoP = ""
var medicacao = [AnyObject]()
override func viewDidLoad() {
self.tableview.delegate = self
self.tableview.dataSource = self
}
@IBAction func btnDismiss(sender: UIButton) {
self.delegate?.receiveRemedios(medicacao)
self.dismissViewControllerAnimated(true, completion: nil)
}
I really do not know what I'm doing wrong. Already I tried all the alternatives I found on stackoverflow and nothing worked.