-1

I have a view controller which is on my story board. The following is the code for view controller;

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

}

now i am accessing this view controller from another view like this;

  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)

The problem is when i instantiate the SingleLineGraphController from story board it doesn't call the viewdidload and hence the lineGraph becomes nil until the point where i call

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

and hence it gives me an exception. I have commented out that line and put a breakpoint on viewdidload and find out that once the below line is executed , it loads the lineGraph.

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

Does anyone have any idea how can i force the viewdidload before the above line so that my method doesn't crash.

ios developer
  • 198
  • 19
neena
  • 365
  • 1
  • 6
  • 22

3 Answers3

0

Just call:

singleLineGraphController.view

After this line:

let singleLineGraphController : SingleLineGraphController = mainStoryboard.instantiateViewControllerWithIdentifier("SingleLineGraphController") as! SingleLineGraphController
Muhammad Waqas Bhati
  • 2,775
  • 20
  • 25
Aruna Mudnoor
  • 4,795
  • 14
  • 16
0

if you will not push your VC to NavigationController, its viewDidLoad will not called viewDidLoad will only called before coming on to the screen, Then viewWillAppear and then viewDidAppear.

VC will only come on to the screen if you push or present or set it as rootVC of NVC and load that NVC as rootVC of window or set it directly to rootVC of window

Hitendra Solanki
  • 4,871
  • 2
  • 22
  • 29
0

In your line:

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

You are pushing a navigationController, not your singleLineGraphController. Are you sure this is correct or did you leave some code out?

Even if you present the controller correctly, because you trying to plot your data before the view is presented the view is not accessible yet. One way to correct this is to set your data to plot on the controller as a property and only plot that data in your viewDidLoad when you know for sure the view is available.

You could access the view property to force the view to load before the controller is actually presented but this is a wrong solution to this problem.

Joris Kluivers
  • 11,894
  • 2
  • 48
  • 47
  • Thanks. Just an update to call view property could be a hack but not a proper solution. – neena Jan 07 '16 at 13:18