Skip to main content
 首页 » 操作系统

ios之将一个变量从一个 View Controller 传递到另一个 View Controller

2024年01月01日41zhujiabin

我是 swift 语言的新手,知道这个问题是重复的。我发现了几个类似的问题和答案,但我无法找出问题所在。

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

ScanViewcontroller 如下:

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 如下:

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 获取变量?

请您参考如下方法:

这听起来可能很奇怪,但您的代码没有任何问题,但它无法正常工作。我以相同的方式使用了您的代码,但它忽略了 segue。然后我将 ScanViewController 嵌入到 Storyboard的导航 Controller 中。我还在 ScanViewController viewDidLoad 中调用了 self.performSegueWithIdentifier("MySegue", sender: self) 以启动 segue。然后一切都像一个魅力。您的 prepareForSegue 没问题。 Yuvrajsinh 的建议很好但不是必需的(我在将 DetailVC 更改为 ResultViewController 后尝试过)。没有导航 Controller 就什么都做不了。 segue.identifier 是一个字符串,它将用于直接的 Swift 字符串比较。

这是 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:

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