![](/img/trans.png)
[英]How to pass an image from one viewcontroller to another viewcontroller in swift?
[英]Pass one variable from one viewcontroller to another viewcontroller
我是一個快速語言的新手,知道這個問題是重復的。我發現了幾個類似的問題和答案,但我無法弄清楚問題。
我想從ScanViewController傳遞detectString變量的值到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.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
}
}
有三種選擇:
prepareForSegue
方法獲取目標ViewController並設置屬性 這可能聽起來很奇怪,但你的代碼沒有任何問題,但它不能正常工作。 我使用了相同的代碼而忽略了segue。 然后我將ScanViewController
嵌入到故事板中的導航控制器中。 我還在ScanViewController viewDidLoad
調用self.performSegueWithIdentifier("MySegue", sender: self)
來啟動segue。 然后一切都像魅力一樣。 你的prepareForSegue很好。 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()
}
}
並為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()
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.