2

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.

2
  • 1
    You are creating a new instance of ViewController in ViewController2 Commented Aug 2, 2016 at 14:24
  • How would I go about it differently? Thanks in advance :) Commented Aug 2, 2016 at 14:25

2 Answers 2

1

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()
}
Sign up to request clarification or add additional context in comments.

4 Comments

This makes the app crash whenever I swap to that view by opening the inventory
can you put here the code, where you are navigating to ViewController2
I am using the tutorial from this website on transitioning from two view controllers mathewsanders.com/animated-transitions-in-swift All that is shown in my code is just the exit segue since I did the present modally segue in my storyboard
Okay so there was no crashing after updating the code but how do I now access the bagCenter variable in viewcontroller2 from this?
1

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)
    }
}

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.