Olexiy  Pyvovarov
Olexiy Pyvovarov

Reputation: 890

Swift exchange variables between View and its ViewController

I have a ViewController:

class graphicViewController: UIViewController {
    var pathToFile = "" //it's changed in method PrepareForSegue from previous ViewController
}

and I have a View:

class graphicView: UIView {
   var ltr:String?
   override func drawRect(rect: CGRect) {
      println(ltr!)
   }
}

How and where should I implement access/transfer of variable pathToFile that it must not be null in drawRect?

Upvotes: 0

Views: 80

Answers (1)

Mike S
Mike S

Reputation: 42335

Since you're setting your pathToFile property in prepareForSegue, the best place to pass the value of the property on would be would be in graphicViewController's viewDidLoad function:

override func viewDidLoad() {
    if let gView = view as? graphicView {
        gView.ltr = pathToFile
    }
}

Sidenote: Typically you'll want to name your types with a capital letter and your instances with a lower case letter. So, graphicView would be GraphicView, etc.

Upvotes: 1

Related Questions