I have been stuck on this for the past few days -
graphView
is my UI Class instance - I declare my CalculatorViewDataSource protocol outside of the GraphView
class, which contain weak var calcDataSource: CalculatorViewDataSource?
in it.
in my GraphViewController, in the didSet{} of the graphView outlet I try to set the delegate to what I assume is an existing CalculatorViewController using the following code:
if let vc = splitViewController?.viewControllers {
println(vc)
println(vc.count)
if let fst = vc.first {
println(fst.subviews)
println(fst.subviews.first)
}
}
graphView.calcDataSource = splitViewController!.viewControllers.first!.subviews.first as CalculatorViewController
the code compile, but I crash with "unexpectedly found nil" when I get to the assignment of graphView.calcDataSource
(output)
[<UINavigationController: 0x7b71bd10>] 1 nil fatal error: unexpectedly found nil while unwrapping an Optional value
(storyboard)
SplitViewController - NavigationController - (Master) CalculatorViewController
I
NavigationController - (Detail) GraphViewController`
========
I worked around this issue by working from inside prepareForSegue (with a storyboard segue on button called "Show Graph")
//CalculatorViewController.swift:
override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject!) {
if (segue!.identifier == "Show Graph") {
var yourNextViewController = (segue!.destinationViewController as UINavigationController)
var detail = yourNextViewController.viewControllers[0] as GraphViewController
var tempview = detail.view // FORCES THE VIEW object into existence, without this it will compile, but next line will crash at runtime (graphView nil)
detail.graphView.calcDataSource = self
}
}
note that var tempview = detail.view
is critical here, despite not being used. I understand as it setting up the view and outlet..
How to (safely) unwrap optionals:
graphView.calcDataSource = splitViewController!.viewControllers.first!.subviews.first as CalculatorViewController
if let vc = splitViewController?.viewControllers {
println(vc)
if let fst = vc.first {
println(fst.subviews)
println(fst.subviews.first)
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.