将一个变量从一个viewcontroller传递给另一个viewcontroller [英] Pass one variable from one viewcontroller to another viewcontroller

查看:98
本文介绍了将一个变量从一个viewcontroller传递给另一个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.

我想从ScanViewController将detectString变量的值传递给ResultViewController。

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

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

ResultViewController如下:

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

println(detectedString)没有给我任何结果。问题是如何从ScanViewController获取变量?

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

推荐答案

这可能听起来很奇怪但你的代码没有任何问题但是它不起作用。我使用了相同的代码而忽略了segue。然后我在故事板中的导航控制器中嵌入了 ScanViewController 。我还在 ScanViewController viewDidLoad 中调用了 self.performSegueWithIdentifier(MySegue,sender:self)来启动塞古。然后一切都像一个魅力。你的prepareForSegue很好。 Yuvrajsinh的建议很好,但没有必要(我在将DetailVC更改为ResultViewController后尝试了)。没有导航控制器没有任何作用。 segue.identifier 是一个字符串,它将在一个直接的Swift字符串比较中工作。

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.

这是代码ScanViewController:

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


}

和ResultViewController:

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

这篇关于将一个变量从一个viewcontroller传递给另一个viewcontroller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆