我有一个 View Controller :
class graphicViewController: UIViewController {
var pathToFile = "" //it's changed in method PrepareForSegue from previous ViewController
}
我有一个 View :
class graphicView: UIView {
var ltr:String?
override func drawRect(rect: CGRect) {
println(ltr!)
}
}
我应该如何以及在哪里实现变量 pathToFile 的访问/传输,它在 drawRect 中不能为 null?
最佳答案
由于您在 prepareForSegue
中设置了 pathToFile
属性,因此传递属性值的最佳位置是 graphicViewController
的 viewDidLoad
函数:
override func viewDidLoad() {
if let gView = view as? graphicView {
gView.ltr = pathToFile
}
}
旁注:通常,您希望使用大写字母命名您的类型,并使用小写字母命名您的实例。因此,graphicView
将是 GraphicView
等
关于ios - Swift 在 View 和它的 ViewController 之间交换变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26492360/