Pass one variable from one viewcontroller to another viewcontroller

后端 未结 3 979
名媛妹妹
名媛妹妹 2020-12-12 03:53

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

相关标签:
3条回答
  • 2020-12-12 04:23

    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
        }
    }
    
    0 讨论(0)
  • 2020-12-12 04:37

    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()
        }
    }
    
    0 讨论(0)
  • 2020-12-12 04:37

    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
    0 讨论(0)
提交回复
热议问题