viewdidload 没有调用
viewdidload is not calling
我的故事板上有一个视图控制器。以下是视图控制器的代码;
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,这样我的方法就不会崩溃。
只需调用:
singleLineGraphController.view
这一行之后:
let singleLineGraphController : SingleLineGraphController = mainStoryboard.instantiateViewControllerWithIdentifier("SingleLineGraphController") as! SingleLineGraphController
如果您不将 VC 推送到 NavigationController,则不会调用其 viewDidLoad
viewDidLoad 只会在进入屏幕之前调用,
然后是 viewWillAppear,然后是 viewDidAppear。
VC 仅当您 按下 或 存在 或 设置时才会出现在屏幕上as rootVC of NVC 并载入 NVC as rootVC of window 或者 直接设置到 window
的根VC
在你的行中:
self.navigationController?.pushViewController(navigationController, animated: false)
您正在推动导航控制器,而不是您的 singleLineGraphController
。您确定这是正确的还是您遗漏了一些代码?
即使您正确呈现控制器,因为您试图在呈现视图之前绘制数据,所以视图尚不可访问。纠正此问题的一种方法是将数据设置为在控制器上绘制为 属性,并且仅当您确定视图可用时才在 viewDidLoad
中绘制该数据。
您可以访问 view
属性 以在实际显示控制器之前强制加载视图,但这是解决此问题的错误方法。
我的故事板上有一个视图控制器。以下是视图控制器的代码;
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,这样我的方法就不会崩溃。
只需调用:
singleLineGraphController.view
这一行之后:
let singleLineGraphController : SingleLineGraphController = mainStoryboard.instantiateViewControllerWithIdentifier("SingleLineGraphController") as! SingleLineGraphController
如果您不将 VC 推送到 NavigationController,则不会调用其 viewDidLoad viewDidLoad 只会在进入屏幕之前调用, 然后是 viewWillAppear,然后是 viewDidAppear。
VC 仅当您 按下 或 存在 或 设置时才会出现在屏幕上as rootVC of NVC 并载入 NVC as rootVC of window 或者 直接设置到 window
的根VC在你的行中:
self.navigationController?.pushViewController(navigationController, animated: false)
您正在推动导航控制器,而不是您的 singleLineGraphController
。您确定这是正确的还是您遗漏了一些代码?
即使您正确呈现控制器,因为您试图在呈现视图之前绘制数据,所以视图尚不可访问。纠正此问题的一种方法是将数据设置为在控制器上绘制为 属性,并且仅当您确定视图可用时才在 viewDidLoad
中绘制该数据。
您可以访问 view
属性 以在实际显示控制器之前强制加载视图,但这是解决此问题的错误方法。