-1

Does anyone can explain me why my NSMutableArray is empty when I'm trying to access to it from another class?

MasterViewController

class MasterViewController: UITableViewController {
    var mutOb:NSMutableArray = NSMutableArray()

    override func viewDidLoad() {
        var test: String = "test"
        self.mutOb.insertObject(test, atIndex: self.mutOb.count)
        super.viewDidLoad()
        println(self.mutOb)
        // RESULT ( test )
    }
}

DetailViewController

class DetailViewController: UIViewController {

    var masterViewController: MasterViewController = MasterViewController(nibName: nil, bundle: nil)
    override func viewDidLoad() {

        super.viewDidLoad()
        println(self.masterViewController.mutOb)

        // RESULT ( )
    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
seek
  • 1,065
  • 16
  • 33

1 Answers1

0

You are creating a new instance of MasterViewController, not referring back to the existing instance. See the attached link for information about passing data between controllers: link

Community
  • 1
  • 1
pbasdf
  • 21,386
  • 4
  • 43
  • 75
  • Do you have any other link that can me explain this for swift? – seek Oct 07 '14 at 16:56
  • I can't find any examples in swift. Only using segues to pass variables but I don't want to use segue for this. – seek Oct 07 '14 at 17:20
  • Sorry, only google... It looks like you are using a Master/Detail application - if that's based on Apple's template, try looking at AppDelegate.swift to see how you might get a reference to the MasterViewController. – pbasdf Oct 07 '14 at 17:21