Ios Swift在视图和视图控制器之间交换变量
我有一个ViewController:Ios Swift在视图和视图控制器之间交换变量,ios,swift,Ios,Swift,我有一个ViewController: class graphicViewController: UIViewController { var pathToFile = "" //it's changed in method PrepareForSegue from previous ViewController } 我有一个观点: class graphicView: UIView { var ltr:String? override func drawRect(rect
class graphicViewController: UIViewController {
var pathToFile = "" //it's changed in method PrepareForSegue from previous ViewController
}
我有一个观点:
class graphicView: UIView {
var ltr:String?
override func drawRect(rect: CGRect) {
println(ltr!)
}
}
我应该如何以及在何处实现对变量pathToFile的访问/传输,使其在drawRect中不能为null?因为您正在
PrepareForegue
中设置pathToFile
属性,传递属性值的最佳位置是graphicViewController
的viewDidLoad
函数:
override func viewDidLoad() {
if let gView = view as? graphicView {
gView.ltr = pathToFile
}
}
旁注:通常,您希望用大写字母命名类型,用小写字母命名实例。因此,
graphicView
将是graphicView
,等等。是graphicView
中的类型视图吗?@Mike,graphicView
是graphicViewController
的子视图。我可以像这样访问graphicView:graphicViewController.view作为graphicView