I'm building an app using swift. In my app, I need to present a UIPopoverPresentationController, and I also need to acces the content controller from that popover from other methods in my normal view controller.
To do this, I would normally in objective C just create a global pointer to my view controller, which would allow me to access it from any method.
This is how I would do it in Swift:
class Categories: UITableViewController, UIPopoverPresentationControllerDelegate {
let storyBoard = UIStoryboard(name: "Main", bundle:nil)
var newCategory = self.storyBoard.instantiateViewControllerWithIdentifier("NewCategory") as NewCategory
//rest of my code
When I do this, Xcode gives me the error:
Categories.type does not have a member named 'storyBoard'
Can anybody tell me what I'm doing wrong and how I need to modify my code to create a legit global pointer to my view controller? Any help would be highly appreciated!
You should use @lazy attribute for this. Bellow is general receipt:
class MyClass {
let compileTimeProperty = "compileTimePropert"
@lazy var runTimeProperty:String = {
return self.compileTimeProperty
}()
}
And here is how your code should be adjusted:
class Categories: UITableViewController, UIPopoverPresentationControllerDelegate {
let storyBoard = UIStoryboard(name: "Main", bundle:nil)
@lazy var newCategory: NewCategory = {
return self.storyBoard.instantiateViewControllerWithIdentifier("NewCategory") as NewCategory
}()
//rest of your code
}