Ios 将一个变量从一个viewcontroller传递到另一个viewcontroller
我不熟悉swift语言,知道这个问题是重复的。我发现了几个类似的问题和答案,但我无法解决这个问题 我想将detectionString变量的值从ScanViewController传递给ResultViewController ScanViewcontroller如下所示:Ios 将一个变量从一个viewcontroller传递到另一个viewcontroller,ios,swift,Ios,Swift,我不熟悉swift语言,知道这个问题是重复的。我发现了几个类似的问题和答案,但我无法解决这个问题 我想将detectionString变量的值从ScanViewController传递给ResultViewController ScanViewcontroller如下所示: import UIkit class ScanViewController: UIViewController { var detectionString : String! override fun
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()
}
}
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()
}
}
结果视图控制器如下所示:
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()
}
}
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.identifier==“MySegue”
是一种如何不以应有的方式进行比较的方法
用下面的函数替换代码,就完成了
override func prepareForSegue(segue: UIStoryboardSegue,sender: AnyObject!) {
let segueName: String = segue.identifier!;
if (segueName == "MySegue") {
var svc = segue.destinationViewController as DetailVC;
svc.detectedString = detectionString
}
}
您有三个选择:
prepareforsgue
方法获取目标ViewController并设置属性这听起来可能很奇怪,但代码没有问题,但它不能正常工作。我使用了相同的代码,它忽略了segue。然后我将
ScanViewController
嵌入到故事板中的导航控制器中。我还调用了ScanViewController viewDidLoad
中的self.performsguewithidentifier(“MySegue”,sender:self)
来启动segue。然后一切都像一个符咒。你的准备工作很好。Yuvrajsinh的建议很好,但没有必要(我在将DetailVC更改为ResultViewController后尝试过)。没有导航控制器,一切都无法工作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()
}
}
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()
}
}
对于ResultViewController:
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()
}
}
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()
}
}
可能重复感谢您的回答,如果我通过@BIAction触发segue,我可以从第一个值中获得值
@IBAction func showssecond(发送者:AnyObject){self.performsguewithidentifier(“MySegue”,发送者:nil)}
每当我试图以编程方式触发segue时,它就会崩溃。感谢您的回答,非常感谢您的帮助,它按照您描述的方式工作。除此之外,我还尝试使用appdelegate获取变量的访问值。再次感谢。谢谢,我试过了,它对我也很有效。