IPhonick 9
IPhonick 9

Reputation: 45

How to access variable from one class in another? Swift Code

I am having an issue getting one simple variable from one class to another and it is beyond extremely frustrating :/...

Here is the deal I have two view controller classes named: ViewController.swift and ViewController2.swift

ViewController.swift is linked to a storyboard that has sort of an inventory bag that I have placed which is just an image. Then there is an @IBAction for when you click on the bag it opens up and the second storyboard pops into view. This is controlled by ViewController2.swift. All I am looking to do is simply pass the center of the bag image from ViewController to ViewController2 but I can't seem to get it to work any help would be greatly appreciated.

class ViewController: UIViewController {
    @IBOutlet weak var InventoryBag: UIImageView!
    var bagCenter:CGPoint = CGPoint(x: 0, y: 0)

    override func viewDidLoad() {
        super.viewDidLoad()

        @IBAction func InventoryButtonTapped(sender: UIButton) {
            self.InventoryBag.image = UIImage(named: "backpackimageopen")
            bagCenter = self.InventoryBag.center

        func transferViewControllerVariables() -> (CGPoint){
            return bagCenter
            }

When I print the bagCenter from this ViewController it works properly and gives me a correct value. So the bagCenter variable I would like to somehow pass over to ViewController2.swift.

Here is what I tried from ViewController2.swift but it never seems to work and always gives me a 0 rather than the actual value.

class ViewController2: UIViewController {

@IBOutlet weak var HideButton: UIButton!
@IBOutlet weak var InventoryCollection: UICollectionView!



override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.

    //Load the center of the inventory bag to this view controller to align the hide button.
    var bagCenter = ViewController().transferViewControllerVariables()
}

But when I do this it always results in a 0 and I don't get the actual coords of the bag that are showing up in ViewController1.

Upvotes: 2

Views: 6539

Answers (2)

Mohammed Shakeer
Mohammed Shakeer

Reputation: 1504

Create a following variable in ViewController2

var previousViewController: ViewController!

Add following line of code in your ViewController Class

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){

    if segue.destinationViewController .isKindOfClass(ViewController2){
      let vc2 = segue.destinationViewController as! ViewController2
        vc2.previousViewController = self
    }
}

Now in viewDidLoad method of ViewController2 you can access bagCenter like below:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.


    var bagCenter = previousViewController.transferViewControllerVariables()
}

Upvotes: 1

Anil Kumar
Anil Kumar

Reputation: 955

Try this in view Controller

class ViewController: UIViewController { 

 @IBOutlet weak var InventoryBag: UIImageView!
    var bagCenter:CGPoint = CGPoint(x: 0, y: 0)

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    @IBAction func InventoryButtonTapped(sender: AnyObject) {
        self.InventoryBag.image = UIImage(named:"MAKEUP_SHARE.jpg")
        bagCenter = self.InventoryBag.center
        performSegueWithIdentifier("Go", sender: self)
    }


    // Give a segue identifier in storyboard
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "yoursegueidentifier" {
            let dvc =  segue.destinationViewController as? ViewController2
            dvc!.bagCenter = bagCenter
        }
    }
}

and in view controller2

class ViewController2: UIViewController {
    var bagCenter:CGPoint!
    override func viewDidLoad() {
        super.viewDidLoad()
        print(bagCenter)
    }
}

Upvotes: 1

Related Questions