Ios 在segue之后查看出口零
我正在参加斯坦福IOS在线开发课程,有一个bug我不知道如何处理。当我在分割视图控制器中切换到详图视图时,我能够从插座didSet中的详图UIViewController设置详图UIView的参数。稍后,当我尝试从控制器访问或修改细节视图时,视图变量(outlet)为零,即使视图仍在屏幕上并响应手势。 这是分割视图控制器中主视图的准备方法Ios 在segue之后查看出口零,ios,cocoa-touch,Ios,Cocoa Touch,我正在参加斯坦福IOS在线开发课程,有一个bug我不知道如何处理。当我在分割视图控制器中切换到详图视图时,我能够从插座didSet中的详图UIViewController设置详图UIView的参数。稍后,当我尝试从控制器访问或修改细节视图时,视图变量(outlet)为零,即使视图仍在屏幕上并响应手势。 这是分割视图控制器中主视图的准备方法 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { let d
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
}
}
}
这是详图视图中的相关变量
var function: ((Double) -> Double)? {
didSet{
setNeedsDisplay()
}
}
任何帮助都将不胜感激,希望这不是一个愚蠢的问题。如果更多的信息会有帮助,让我知道。
谢谢 它将为零,因为在推送viewcontrolle时您的视图未完全加载。因此,您需要通过以下考试: 在图形视图控制器中
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
}
}
非常感谢你,真是帮了大忙。