0

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.

cb0
  • 7,631
  • 9
  • 49
  • 76
Tatiana
  • 3
  • 2
  • Use an unwind segue. Case Solved. http://stackoverflow.com/questions/12561735/what-are-unwind-segues-for-and-how-do-you-use-them – Coder1000 May 07 '16 at 17:38
  • You should declare the protocol in the second VC and conform to it in first VC. – Maysam May 07 '16 at 17:42

2 Answers2

0

You seem to have forgot to set the delegate of the presented VC.

let vc : MedicacaoPopUp = storyboard.instantiateViewControllerWithIdentifier("MedicacaoPopUp") as! MedicacaoPopUp

Add

vc.delegate = self
Ayush Goel
  • 3,086
  • 25
  • 35
0

Really a well structured code. That's a good idea to use a protocol and delegate as callback returning data from the popup.

Just one thing... Where is this

vc.delegate = self

In the first code snippet?

NoImaginationGuy
  • 1,669
  • 10
  • 22
  • Thank you ... Thank you ... it was this line that was missing for everything to work perfectly ...: D – Tatiana May 07 '16 at 18:18