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?

1

3 Answers 3

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

1 Comment

,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.
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
    }
}

1 Comment

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.
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

1 Comment

thank you, I tried delegate and it works for me too.

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.