Rustam
Rustam

Reputation: 192

Pass one variable from one viewcontroller to another viewcontroller

I new to swift language and know this question is duplicated.I`ve found several similar question and answer, but i could not able to figure out the problem.

I want to pass the value of detectionString variable to ResultViewController from ScanViewController.

ScanViewcontroller as below:

import UIkit

class ScanViewController: UIViewController  {

    var detectionString : String!

    override func viewDidLoad() {
        super.viewDidLoad()

        detectionString = “SomeDetectedString”
    }

    override func prepareForSegue(segue: UIStoryboardSegue,sender: AnyObject!) {
        if (segue.identifier == "MySegue") {
            var svc = segue.destinationViewController as ResultViewController;
            svc.detectedString = detectionString
        }
    }

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

The ResultViewController as below:

import UIkit

class ResultViewController: UIViewController {

    var detectedString: String!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor=UIColor.whiteColor()
        println(detectedString)
    }

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

The println(detectedString) gives me no result. question is that how can get the variable from ScanViewController?

Upvotes: 1

Views: 3385

Answers (3)

Syed Tariq
Syed Tariq

Reputation: 2928

This may sound bizarre but there is nothing wrong with your code BUT it does not work as is. I used your code identically and it ignored the segue. Then I embedded ScanViewController in a navigation controller in the storyboard. I also put a call to self.performSegueWithIdentifier("MySegue", sender: self) in the ScanViewController viewDidLoad to initiate the segue. Then everything works like a charm. Your prepareForSegue is fine. Yuvrajsinh's suggestion is fine but not necessary (I tried it after changing DetailVC to ResultViewController). Without the navigation controller nothing works. segue.identifier is a string and it will work in a straight Swift string comparison.

Here is the code for ScanViewController:

import UIkit

class ScanViewController: UIViewController  {

    var detectionString : String!

    override func viewDidLoad() {
        super.viewDidLoad()


        detectionString = "SomeDetectedString"
        println(detectionString)
        self.performSegueWithIdentifier("MySegue", sender: self)
    }

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

        if (segue.identifier == "MySegue" || segue.identifier == "SegueFromButton") {
            println("prepareForSegue")
            var svc = segue.destinationViewController as ResultViewController;
            svc.detectedString = detectionString
            println("svc.detectedString: \(svc.detectedString)")
        }
    }

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


}

and for ResultViewController:

import UIkit

class ResultViewController: UIViewController {

    var detectedString: String!

    override func viewDidLoad() {
        println("Result Load View")
        super.viewDidLoad()
        self.view.backgroundColor=UIColor.whiteColor()
        println("detectedString in Result: \(detectedString)")
    }

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

Upvotes: 1

duan
duan

Reputation: 8885

There are three options for you:

  1. use prepareForSeguemethod to get the target ViewController and set up the property
  2. set up delegate relationship between the two ViewControllers to communicate
  3. set up Observer with NSNotificationCenter

Upvotes: 1

Yuvrajsinh
Yuvrajsinh

Reputation: 4584

your segue.identifier == "MySegue" is some how not comparing in the way it should.

replace your code with following function and you are done.

override func prepareForSegue(segue: UIStoryboardSegue,sender: AnyObject!) {
    let segueName: String = segue.identifier!;
    if (segueName == "MySegue") {
        var svc = segue.destinationViewController as DetailVC;
        svc.detectedString = detectionString
    }
}

Upvotes: 1

Related Questions