1

I am working on an app where i needed to move between view controllers a lot of time mostly making a loop but now i have to store some data(simple variable's and array's).

I am currently storing them in the app delegate but i don't know if this is a great idea. i have looked online but i couldn't really tell what is the best solution for me.
This is how i have it set up: Appdelegate:

var aantalSpelers: Int!
var namenSpelers = [String]()

var youself = KaartenSpeler()
var player2 = KaartenSpeler()
var player3 = KaartenSpeler()
var player4 = KaartenSpeler()
var player5 = KaartenSpeler()
var player6 = KaartenSpeler()

var Vragen = [[0,0,0,0,0,5]]
var VragenOnbekend = [[6,0,0,0,0,0]]
var VragenInformatie = [[["Spelletjeskamer",""],["Keuken",""],["Garage",""],["Binnenplaats",""],["Zitkamer",""],["Slaapkamer",""],["Studeerkamer",""],["Eetkamer",""],["Badkamer",""]], [["De Wit",""],["Pimpel",""],["Blaauw van Draet",""],["Roodhart",""],["Groenewoud",""],["Van Geelen",""]], [["Loden pijp",""],["Pistool",""],["Engelse sleutel",""],["Dolk",""],["Touw",""],["Kandelaar",""]]]
var EersteKeerMainScreen = true

and in the VC:

func Appdelegate() -> AppDelegate {
    return UIApplication.sharedApplication().delegate as! AppDelegate
}

let sections = ["Locaties","Personages","Wapens"]

var aantalSpelers: Int!
var namenSpelers = [String]()
var eersteKaarten = [[Int(),Int()]]

var youself: KaartenSpeler!
var player2: KaartenSpeler!
var player3: KaartenSpeler!
var player4: KaartenSpeler!
var player5: KaartenSpeler!
var player6: KaartenSpeler!

//vraag is: [numberVrager,numerGevraagde,numerLocatie,numerPersonage,numerWapen,0=lietgeenkaartzien - 1=locatie, - 2=personage - 3=wapen - 4=onbekend]
var Vragen = [[]]
var VragenOnbekend = [[]]
var VragenInformatie = []

var EersteKeerMainScreen = false

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    //Get information back from appdelegate
    aantalSpelers = Appdelegate().aantalSpelers
    namenSpelers = Appdelegate().namenSpelers

    youself = Appdelegate().youself
    player2 = Appdelegate().player2
    player3 = Appdelegate().player3
    player4 = Appdelegate().player4
    player5 = Appdelegate().player5
    player6 = Appdelegate().player6

    Vragen = Appdelegate().Vragen
    VragenOnbekend = Appdelegate().VragenOnbekend
    VragenInformatie = Appdelegate().VragenInformatie
    EersteKeerMainScreen = Appdelegate().EersteKeerMainScreen

And is this actually a viable option?

P.S. sorry for my bad english

Cing
  • 806
  • 1
  • 11
  • 29
  • Possible duplicate of [Using the AppDelegate to share data](http://stackoverflow.com/questions/12046060/using-the-appdelegate-to-share-data) –  Jan 25 '16 at 23:16
  • If you're having problems with @ksa_coder's answer you can follow my video tutorial here. https://www.codebeaulieu.com/29/prepareForSegue . If you get it working make sure you give ksa_coder credit! – Dan Beaulieu Jan 25 '16 at 23:30
  • For that i need to read your tutorial a bit more but i am not quite sure how i could set it up with multiple vc and even a navigation controller. – Cing Jan 25 '16 at 23:39
  • And i will up vote Ksa_coder after i am done. ;) – Cing Jan 25 '16 at 23:40
  • I am try to get the protocol delegate to work on a other projec. But how would i get this to work with navigation controller? – Cing Feb 07 '16 at 20:02
  • Update: got it working using this tutorial [link](http://makeapppie.com/2014/07/01/swift-swift-using-segues-and-delegates-in-navigation-controllers-part-1-the-template/) Your tutorial still helped dough :) – Cing Feb 07 '16 at 20:57

2 Answers2

2

The approach I would use (may not be the best) in this case is to pass the variables with you using segues through the use of self.performSegueWithIdentifier("segueTitle", sender).

Then you can in a prepareForSegue actually handle the objects you moving from this VC to another VC.

e.g.

/*Global Variable is numbersArray*/
var numbersArray: [String] = []

and whenever you finish writing your logic for the view controller, use:

self.performSegueWithIdentifier("segueTitle", sender: self)

Now to handle what you want to pass to the next VC, add this:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
        if (segue.identifier == "segueTitle") {
            // pass data to next view
            let destinationVC = segue.destinationViewController as! YourNextViewController
            destinationVC.numbersArray = self.numbersArray;

    }
}

Things to keep in mind is that you must have the same global variable in next VC as you can see we are assigning current to next in the above example. Also make sure that segue is created and connected via Storyboard and replace segueTitle with title given to it.

Update: E.g. TransferClass

class TranferClass {
   var numbersArray:[String] = []

}

then instantiate this class and send it to next VC.Eg.:

in VC 1:

/*in global scope*/
var transferObject = TransferClass()

in the same VC, set the array for example:

self.transferObject.numbersArray[0] = "Hello"

then trigger the segue:

self.performSegueWithIdentifier(...)

then handle the passing:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
            if (segue.identifier == "segueTitle") {
                // pass data to next view
                let destinationVC = segue.destinationViewController as! YourNextViewController
                destinationVC.transferObject = self.transferObject;

        }
    }
ksa_coder
  • 1,393
  • 3
  • 15
  • 38
  • I have thought of that but i don't need all the variables on most of the vc. I have 1 main screen and then i move to others where i just add 1 thing to the array's – Cing Jan 25 '16 at 23:03
  • @Cing I create a swift class with multiple variables of different types in it. Then this causes it to be cleaner in code and less variables to manage. so then you pass an instance of the class and set variables when and where needed. – ksa_coder Jan 25 '16 at 23:05
  • I don't really get what you're saying – Cing Jan 25 '16 at 23:06
  • @Cing This is better than the other option. Don't create singletons for your variables passing them along is better. I would personally opt for the protocol-delegate pattern: https://www.codebeaulieu.com/36/Passing-data-with-the-protocol-delegate-pattern – Dan Beaulieu Jan 25 '16 at 23:17
  • @DanBeaulieu thanks for providing more options to help :) – ksa_coder Jan 25 '16 at 23:18
  • @DanBeaulieu yes I agree on opting for not creating singletons – jasonnoahchoi Jan 25 '16 at 23:29
  • What is actually the real difference between passing the data between protocol delegate and segue? – Cing Jan 25 '16 at 23:33
0

Your initial thought in that you probably shouldn't store these in the AppDelegate. I recommend either creating a new class or a struct that can hold these values.

But the other thing I noticed is that you have a separate class already for KaartenSpeler so you definitely want to look out for retain cycles and make sure that these classes don't hold strong references to one another.

jasonnoahchoi
  • 871
  • 5
  • 16
  • where should i put the struct and how should i code it? – Cing Jan 25 '16 at 23:13
  • @Cing he's talking about the singleton pattern. Here's a good tutorial: http://krakendev.io/blog/the-right-way-to-write-a-singleton – Dan Beaulieu Jan 25 '16 at 23:19
  • How do i know if it has a strong references cycle and how could i avoid it – Cing Jan 25 '16 at 23:20
  • If you're referencing another class from your class you're potentially creating a strong reference. just use "weak var" when creating a variable that will hold a class object – Dan Beaulieu Jan 25 '16 at 23:20