0

I'm new in swift, and I've been searching this answer for weeks, but I still haven't found it:( What I'm building is a master-detail app. It is a reminders app.The user is able to edit those reminders in the detail view, but the problem comes when I try to pass the edited data back to the master view, and add it to an array of arrays that I use as the database. I've tried with an unwind segue, but I don't know how to add in the right place the data that I have passed back. I would really appreciate your help!

//MAIN VIEW
var addMoneyArray = [[Int]]()


class GonMainTableViewController: UITableViewController {

    //MARK: - VARIABLES
    var recivedData : Int?

    //MARK. - IB OUTLETS

    @IBOutlet var tableView1: UITableView!
    @IBAction func unwindViewController(segue: UIStoryboardSegue){
        if(segue.sourceViewController .isKindOfClass(GonDetailViewController))
        {
            let view2 : GonDetailViewController = segue.sourceViewController as! GonDetailViewController

          recivedData = view2.passTxtFieldValue


        }
    }




    enter code here

//VIEW2

class GonDetailViewController: UIViewController {

var passTxtFieldValue : Int?

 @IBAction func addBtn(sender: AnyObject) {
        var returnMoney = Int(addMoneyTxtField.text!)
        returnMoney =  passTxtFieldValue


        let alertController = UIAlertController(title: "Awesome!", message: "Succesfully saved your data!", preferredStyle: .Alert)
        let takePhotoAction = UIAlertAction(title: "OK", style: .Default) { (Alert) -> Void in
            self.performSegueWithIdentifier("returnToMain", sender: self)


        }
        alertController.addAction(takePhotoAction)
        presentViewController(alertController, animated: true, completion: nil)

    }
  • Please post your code that shows what you tried. –  Jan 26 '16 at 18:21
  • I have updated the post with the code bellow – Gonzalo Duarte Jan 26 '16 at 18:47
  • The [following answer](http://stackoverflow.com/questions/34247239/global-variable-and-optional-binding-in-swift/34275632#34275632) describes in detail how to pass data from a ViewController (detail in your case; for each cell) to a TableViewController (master) by use of an unwind segue. It should be applicable for your example above. – dfrib Jan 26 '16 at 18:48
  • 1
    Thank you so much I'll try it, and you in a minute! – Gonzalo Duarte Jan 26 '16 at 19:13
  • I have tried it and it seems pretty good, but I have 2 questions: – Gonzalo Duarte Jan 26 '16 at 20:34
  • Sure, just ask away! – dfrib Jan 26 '16 at 20:36
  • That code seems very good, but when I implement it, I dont know where the variable text is coming from. – Gonzalo Duarte Jan 26 '16 at 20:49
  • And the other question is about an error I'm getting, but I think is because I don't know where text is coming from, so I cant make an equivalent on my app – Gonzalo Duarte Jan 26 '16 at 20:53
  • I will assume you are referring to `text = sourceViewController.cellText` in the `unwindToTableView` method? Note that this is part of the single long optional binding line `if let sourceViewController = sender.sourceViewController as? ViewController, text = sourceViewController.cellText`, which optionally binds the unwrapped values (if non-nil) of `sender.sourceViewController as? ViewController` (to `sourceViewController`) and `sourceViewController.cellText` (to `text`). So `text` is just a local immutable that lives in the scope of that `if let` clause. – dfrib Jan 26 '16 at 20:57

0 Answers0