2

I am trying to copy an array from one view controller to another view controller, but somehow it does not work.. I have tried using this code:

let otherVC = TerningspilletViewController()
        mineSpillere = otherVC.mineSpillere2
        println("otherVC")

In the view controller i want to send the data from has this code:

var mineSpillere = ["Spiller 1", "Spiller 2", "Spiller 3"]

The view controller that is going to received this data, has this code:

var mineSpillere2 = [String]()

the "var mineSpillere" is going to show the text, but when i try to show it, it says that the "var mineSpillere" is empty. Any suggestions/ideas?

4
  • 1
    Maybe you shold reverse the mineSpillere = otherVC.mineSpillere2 to otherVC.mineSpillere2 if you want to assign to mineSpillere2? Commented Jul 23, 2015 at 12:24
  • @Laffen - I have done that, but still does not work.. Commented Jul 23, 2015 at 12:30
  • Where is the line var mineSpillere2 = [String]() make sure it's outside of the methods and maybe think about using a ! or ? Commented Jul 23, 2015 at 12:33
  • Your example tells me that var mineSpillere is assigned the value of otherVC.mineSpillere2 which doesn't hold any data. Commented Jul 23, 2015 at 12:36

4 Answers 4

5

If you want to access array of another viewController then you can save it in memory and for that you can use NSUserDefaults this way:

//save
var mineSpillere = ["Spiller 1", "Spiller 2", "Spiller 3"]
var defaults = NSUserDefaults.standardUserDefaults()
defaults.setObject(mineSpillere, forKey: "YourKey")

Now you can read it from anywhere this way:

//read
if let testArray : AnyObject? = NSUserDefaults.standardUserDefaults().objectForKey("YourKey") {
    var readArray : [NSString] = testArray! as! [NSString]
    println(readArray)
}

And if you want to do with your old way here is code:

FirstViewController.swift

import UIKit

class FirstViewController: UIViewController {

    var mineSpillere = ["Spiller 1", "Spiller 2", "Spiller 3"]
    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

SecondViewController.swift

import UIKit

class SecondViewController: UIViewController {

    var mineSpillere2 = [String]()
    override func viewDidLoad() {
        super.viewDidLoad()

        let otherVC = FirstViewController()
        mineSpillere2 = otherVC.mineSpillere
        println(mineSpillere2)  //[Spiller 1, Spiller 2, Spiller 3]
    }

}
Sign up to request clarification or add additional context in comments.

3 Comments

I will have to use the first codes you gave me, using NSUserDefaults, but i get error when trying to load: s28.postimg.org/iwxu0tf4t/…
check your key. It should be same as you are saving your array.
Question about the "readArray". How can i put data from "readArray" to "mineSpillere" at viewDidView.
0

first checkout are you using story board

if you are using storyboard

let otherVc = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle()).instantiateViewControllerWithIdentifier("storyboardIdendifier") as? TerningspilletViewController
otherVC.mineSpillere2 =  mineSpillere 
println(otherVc)

otherwise do like this

let otherVC = TerningspilletViewController()
    otherVC.mineSpillere2 =  mineSpillere
    println("otherVC")

Comments

0

if you are using storyboards, I suggest passing the data in prepareForSegue:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "TerningspilletViewSegue" {
        let otherVc = segue.destinationViewController as! TerningspilletViewController
        otherVc.mineSpillere = mineSpillere
    }     
}

Comments

0

Try this code working fine sending data from one viewController to another viewcontroller.just reference of storyboard id you can send data from one view to another view .Then we create that class object based on that object reference we can access that array and those class properties then just append it.

 let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let secondView = storyBoard.instantiateViewController(withIdentifier: "tableView") as! TableViewController.
self.present(secondView, animated: true) {
    let data = nameArray
    secondView.models.append(data!)
    secondView.tableView.reloadData()
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.