1

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?

PREMKUMAR
  • 8,283
  • 8
  • 28
  • 51
Rustam
  • 182
  • 1
  • 9
  • possible duplicate of [Pass variables from one ViewController to another in Swift](http://stackoverflow.com/questions/24044108/pass-variables-from-one-viewcontroller-to-another-in-swift) – jtbandes Feb 08 '15 at 06:00

3 Answers3

1

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
    }
}
Yuvrajsinh
  • 4,536
  • 1
  • 18
  • 32
  • Thank you for your answer,if i trigger segue by @BIAction , i can get the value from first value. `@IBAction func showsecond(sender: AnyObject) { self.performSegueWithIdentifier("MySegue", sender: nil) }` It crashed whenever i tried to triger segue programatically. – Rustam Feb 08 '15 at 09:01
1

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
duan
  • 8,515
  • 3
  • 48
  • 70
1

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()
    }
}
Syed Tariq
  • 2,878
  • 3
  • 27
  • 37
  • ,Thank you for answer, really appreciate your help , it works as you described. beside this , i also tried to get access value of the variable by using appdelegate. Thank you again. – Rustam Feb 08 '15 at 16:05