viewdidload没有调用

时间:2016-01-07 10:53:37

标签: ios swift storyboard viewdidload

我的故事板上有一个视图控制器。以下是视图控制器的代码;

class SingleLineGraphController: UIViewController {
       @IBOutlet var lineGraph: LineGraphView!
        override func viewDidLoad() {


super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // Mark: GraphDelegate implementation
    func plotLineGraph(xAxisValue:[NSDate],yAxisValue:[Double],displayView:GraphDisplayView, graphTitle:String,graphStartDate:NSDate , graphEndDate:NSDate)
    {
        lineGraph.plotLineGraph(xAxisValue, yAxisValue: yAxisValue, displayView: displayView, graphTitle: graphTitle, graphStartDate: graphStartDate, graphEndDate: graphEndDate)
    }

}

现在我从另一个这样的视图访问这个视图控制器;

  let mainStoryboard = UIStoryboard(name: "Menu", bundle: NSBundle.mainBundle())
        let singleLineGraphController : SingleLineGraphController = mainStoryboard.instantiateViewControllerWithIdentifier("SingleLineGraphController") as! SingleLineGraphController
  let graphData = getGraphData(DashBoardRow.Respiratory, cellTitle: "Respiratory") as! LineGraphModel
        singleLineGraphController.plotLineGraph(graphData.xAxisValue, yAxisValue: graphData.yAxisValue, displayView: graphDisplayView, graphTitle: graphData.cellTitle, graphStartDate: graphData.graphStartDate, graphEndDate: graphData.graphEndDate, latestReadingText: graphData.latestObservationText, latestReadingDate: graphData.latestObservationDate)

self.navigationController?.pushViewController(navigationController, animated: false)

问题是当我从故事板实例化SingleLineGraphController时它没有调用viewdidload,因此lineGraph变为nil直到我调用的地方

 singleLineGraphController.plotLineGraph(graphData.xAxisValue, yAxisValue: graphData.yAxisValue, displayView: graphDisplayView, graphTitle: graphData.cellTitle, graphStartDate: graphData.graphStartDate, graphEndDate: graphData.graphEndDate, latestReadingText: graphData.latestObservationText, latestReadingDate: graphData.latestObservationDate)

因此它给了我一个例外。我已注释掉该行并在viewdidload上设置了一个断点,并发现一旦执行了以下行,它就会加载lineGraph。

self.navigationController?.pushViewController(navigationController, animated: false)

有没有人知道如何在上面的行之前强制使用viewdidload,这样我的方法就不会崩溃。

3 个答案:

答案 0 :(得分:0)

请致电:

singleLineGraphController.view

这一行之后:

let singleLineGraphController : SingleLineGraphController = mainStoryboard.instantiateViewControllerWithIdentifier("SingleLineGraphController") as! SingleLineGraphController

答案 1 :(得分:0)

如果您不将VC推送到NavigationController,其viewDidLoad将不会被调用 viewDidLoad只会在进入屏幕之前调用, 然后viewWillAppear然后viewDidAppear。

如果推送存在将其设置为NVC的rootVC ,则VC只会进入屏幕并将该NVC加载为rootVC窗口或直接将其设置为窗口的rootVC

答案 2 :(得分:0)

在你的行中:

self.navigationController?.pushViewController(navigationController, animated: false)

您正在推送navigationController,而不是您的singleLineGraphController。你确定这是正确的还是你留下了一些代码?

即使您正确显示控制器,因为您尝试在显示视图之前绘制数据,因此无法访问视图。解决此问题的一种方法是将您的数据设置为在控制器上作为属性进行绘制,并且只有在您确定该视图可用时才会在viewDidLoad中绘制该数据。

您可以访问view属性以在实际显示控制器之前强制加载视图,但这是解决此问题的错误方法。