我正在进行在线斯坦福IOS开发课程,我遇到了一个我不知道如何处理的错误。当我切换到拆分视图控制器中的详细视图时,我能够将详细信息UIView的参数从出口的didSet中的详细信息UIViewController设置到视图。稍后,当我尝试从控制器访问或改变详细视图时,即使视图仍在屏幕上并响应手势,视图变量(出口)也为零。 这是拆分视图控制器中主视图的准备方法
Map<Key, List<Value>>
这是详细视图控制器。我评论了有趣的部分。
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destinationViewController = segue.destination
if let graphViewController = destinationViewController as? GraphViewController {
if let identifier = segue.identifier{
switch identifier {
case "DisplayGraph":
graphViewController.function = funcForGraphView(_:)
default:
break
}
}
}
}
这是详细视图中的相关变量
class GraphViewController: UIViewController {
@IBOutlet weak var graphView: GraphView!{
didSet{
graphView.addGestureRecognizer(UITapGestureRecognizer(target: graphView, action: #selector(graphView.tap(recognizer:))))
graphView.addGestureRecognizer(UIPanGestureRecognizer(target: graphView, action: #selector(graphView.pan(recognizer:))))
graphView.addGestureRecognizer(UIPinchGestureRecognizer(target: graphView, action: #selector(graphView.zoom(recognizer:))))
graphView.function = cos //I can set variables in the view here
// After this didSet I am no longer able to set or read any variables from the graphView
}
}
var function: ((Double)->(Double))? {
didSet{
if let newfunction = function {
graphView.function = newfunction // I can not set or access variables in the view here since for some reason graphView is nil
}
}
}
Storyboard Picture 任何帮助将不胜感激,希望这不是一个愚蠢的问题。如果有更多信息有用,请告诉我。 谢谢!
答案 0 :(得分:0)
由于你的视图没有完全加载,因为你的推视控制它将是零。所以你需要传递如下:
在GraphViewController中
var yourValue : ((Double)->(Double))?
override func viewDidLoad() {
super.viewDidLoad()
self.function = yourValue
}
导航时设置值
if let identifier = segue.identifier{
switch identifier {
case "DisplayGraph":
graphViewController.yourValue = funcForGraphView(_:)
default:
break
}
}