【问题标题】:View outlet nil after segue在segue之后查看outlet nil
【发布时间】:2017-07-20 04:16:31
【问题描述】:

我正在学习斯坦福 IOS 开发在线课程,但遇到了一个我不知道如何处理的错误。当我转到拆分视图控制器中的详细视图时,我可以将详细 UIView 的参数从出口的 didSet 中的详细 UIViewController 设置到视图。稍后,当我尝试从控制器访问或改变详细视图时,即使视图仍在屏幕上并响应手势,视图变量(出口)也为零。 这是拆分视图控制器中主视图的prepare方法

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()  
    }  
}  

Storyboard Picture 任何帮助将不胜感激,希望这不是一个愚蠢的问题。如果更多信息会有所帮助,请告诉我。 谢谢!

【问题讨论】:

    标签: ios cocoa-touch


    【解决方案1】:

    它将为零,因为您在推送视图控件时视图未完全加载。所以你需要通过如下方式:

    在 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  
                }  
            } 
    

    【讨论】:

    • 非常感谢您的帮助。
    猜你喜欢
    • 2013-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多