Ios Swift在视图和视图控制器之间交换变量

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

我有一个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: 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