我需要将一个变量从一个ViewController传递给另一个,并在textView中显示该变量的内容。以下代码正确计算了单词的数量(以非常简单的方式),但它没有在textView中显示。有人知道为什么没有显示?:
编辑:添加了一些新代码。现在它在第一个VC中失败,在下一行: DestViewController.num_words = num_words
,它说:“使用未解析的标识符'num_words'。如果我创建一个具有任何值的全局变量,那么它在不改变变量内容的情况下在textView中显示它......
TransViewController
import UIKit
import Foundation
class TransViewController: UIViewController {
@IBOutlet var trad_text: UITextView!
@IBOutlet var buttonTrad: UIButton!
@IBOutlet var labelsitoh: UILabel!
@IBAction func butCalc(_ sender: UIButton) {
let text = trad_text.text
var num_words = (text?.components(separatedBy: " ").count)!-1
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
var DestViewController : PaymentViewController = segue.destination as! PaymentViewController
DestViewController.num_words = num_words
}
override func viewDidLoad() {
super.viewDidLoad()
trad_text!.layer.borderWidth = 1
trad_text!.layer.cornerRadius = 20
trad_text!.layer.borderColor = UIColor(red:0.22, green:0.26, blue:0.39, alpha:1.0).cgColor
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
PViewControler
import UIKit
import Foundation
class PaymentViewController: UIViewController {
@IBOutlet var labelImport: UITextView!
var num_words = String()
override func viewDidLoad() {
super.viewDidLoad()
labelImport.text = num_words
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
非常感谢你的时间