0

I am relatively new to XCode and I have a problem. In my UITableViewController I have an array with a list of tricks.

var trickListArray = [tricktionaryViewController.trick(category: "", name: "", turn: "", degree: 0, description: "", instruction: "")]
    var learningListArray = [tricktionaryViewController.trick(category: "", name: "", turn: "", degree: 0, description: "", instruction: "")]

    var myTricksArray:[[tricktionaryViewController.trick]] {
        return [trickListArray,learningListArray]
    }

I want to append objects of the class "trick" to this array from another ViewController through an "add to trick list" UIButton. After pressing this button a PopUp appears saying "Successfully added", but I don't want to segue to my UITableViewController, but simply append the trick object to my trickListArray or learningListArray and update my UITableView afterwards like this:

@IBAction func addToTrickList(sender: AnyObject) {
        var destinationViewController: myTricksViewController = myTricksViewController(nibName: nil, bundle: nil)
        destinationViewController.trickListArray.append(self.activeTrick)
        var popUpTextString: String = "Successfully added " + String(activeTrick.name) + " to the list"
        self.popViewController.showInView(self.view, animated: true, text: popUpTextString)

    }

if I add a reloadData() function in the IBAction like this:

@IBAction func addToTrickList(sender: AnyObject) {
        var destinationViewController: myTricksViewController = myTricksViewController(nibName: nil, bundle: nil)
        destinationViewController.trickListArray.append(self.activeTrick)
        destinationViewController.myTricksTableView?.reloadData()
        var popUpTextString: String = "Successfully added " + String(activeTrick.name) + " to the list"
        self.popViewController.showInView(self.view, animated: true, text: popUpTextString)

    }

it will not update the UITableView. I have not yet tested whether the array is really updated or not, but it should be.

Any help is appreciated!

Code cracker
  • 3,105
  • 6
  • 37
  • 67

1 Answers1

0

You need to apply some arcitecture! The viewcontroller should not control the data source. Instead, put your data-array in a singleton, and you can add/remove objects to that list from anywhere in your app.

Here's a guide for the architecture in obj-c: http://www.galloway.me.uk/tutorials/singleton-classes/

Here's example of how to do a singleton class in swift: Singleton class for sharing data

Edit:

If you want a fast and easy way, you can use ur appdelegate as the singleton.

Put your data-array as a property in the appdelegate, and get the instance to your appdelegate via this:

let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let yourlist = appDelegate.listOfObjects
Community
  • 1
  • 1
ullstrm
  • 9,812
  • 7
  • 52
  • 83
  • thanks for your answer. The suggestion with the appDelegate did work for me. Could you explain to me a little more how to use a singleton and how I can add properties in there? Do I create a new .swift file and declare a new singleton class? And then..? – timgilbert7331 Mar 09 '15 at 22:21
  • Alright, after trying your suggestion I managed to add data to my array und was able to print it to the console to make sure that data was added. But still I don't seem to be able to update the UITableView in the other ViewController. – timgilbert7331 Mar 10 '15 at 01:04
  • All you need to do is redirect your other viewcontroller's data array to the one in the singleton/appdelegate. Then on viewwillappear you just call reloaddata and you should see new data. – ullstrm Mar 10 '15 at 07:06