我有两个数组:pointHistory
和dateHistory
。我也有NSTimer
,当计时器用完时,会调用其动作。在该操作中,我将Int
追加到pointHistory
,将当前日期追加到string
到dateHistory
。这是计时器操作:
func timerAction() {
let stepperValue = Int(stepper.value)
employee.numberOfPoints = stepperValue
networking.saveEmployee(employee, completion: { (error) -> Void in
if error != nil {
let alert = UIAlertController(title: "Error", message: "There was an error updating \(self.employee.name)'s points", preferredStyle: .Alert)
let alertAction = UIAlertAction(title: "Ok", style: .Cancel, handler: nil)
alert.addAction(alertAction)
self.presentViewController(alert, animated: true, completion: nil)
}
})
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MM-dd-yyyy"
dateFormatter.timeZone = NSTimeZone.localTimeZone()
var date = NSDate()
var dateString = dateFormatter.stringFromDate(date)
//APPENDING HERE
self.pointHistory.append(self.employee.numberOfPoints)
self.dateHistory.append(dateString)
}
但是,当视图加载时,这两个数组会将自身重置为空。这是我的viewDidLoad
方法:
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.whiteColor()
self.title = employee.name
editBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Edit, target: self, action: Selector("edit"))
if let font = UIFont(name: "Avenir", size: 18) {
editBarButtonItem.setTitleTextAttributes([NSFontAttributeName: font], forState: UIControlState.Normal)
}
self.navigationItem.rightBarButtonItem = editBarButtonItem
if let font = UIFont(name: "Avenir", size: 17) {
UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: font], forState: UIControlState.Normal)
}
self.addScrollView()
self.addProfilePicture()
self.addUIElements()
self.addLineChart()
setChart(dateHistory, yVals: pointHistory)
}
dateHistory和pointHistory的重点是我为我的应用添加折线图,所有数据都存储在这些数组中。这是老挝为什么你看到一个setChart函数。对于lineChart,我使用的是iOS图表框架。这是setChart函数的实际主体,因为它在viewDidLoad中调用:
func setChart(xVals: [String], yVals: [Int]) {
var dataEntries = [ChartDataEntry]()
for i in 0..<xVals.count {
let dataEntry = ChartDataEntry(value: Double(yVals[i]), xIndex: i)
dataEntries.append(dataEntry)
}
let lineChartDataSet = LineChartDataSet(yVals: dataEntries, label: "Number of Points")
let lineChartData = LineChartData(xVals: xVals, dataSet: lineChartDataSet)
lineChart.data = lineChartData
}
最后,为了提供更多信息,重置数组的原因可能是因为我实现了viewWillAppear函数。这是:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.refreshView()
}
以下是refreshView
功能:
func refreshView() {
self.networking.findEmployeeForLoggedInUser { (array, error) -> Void in
if error != nil {
let alert = UIAlertController(title: "Error", message: "\(error)", preferredStyle: .Alert)
let alertAction = UIAlertAction(title: "Ok", style: .Cancel, handler: nil)
alert.addAction(alertAction)
self.presentViewController(alert, animated: true, completion: nil)
} else {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.nameLabel.text = self.employee.name
self.title = self.employee.name
self.roleLabel.text = self.employee.jobDesc
self.educationLabel.text = self.employee.education
self.birthdayLabel.text = self.employee.birthday
self.commentaryTextView.text = self.employee.commentary
self.emailTextView.text = self.employee.email
})
}
}
self.networking.fetchImageForEmployee(employee, completion: { (error, image) -> Void in
self.imageActInd.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.White
self.image.addSubview(self.imageActInd)
self.imageActInd.setTranslatesAutoresizingMaskIntoConstraints(false)
self.view.addConstraint(NSLayoutConstraint(item: self.imageActInd, attribute: .CenterX, relatedBy: .Equal, toItem: self.image, attribute: .CenterX, multiplier: 1.0, constant: 0.0))
self.view.addConstraint(NSLayoutConstraint(item: self.imageActInd, attribute: .CenterY, relatedBy: .Equal, toItem: self.image, attribute: .CenterY, multiplier: 1.0, constant: 0.0))
self.imageActInd.startAnimating()
self.image.alpha = 0.5
if error != nil {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.imageActInd.stopAnimating()
self.image.alpha = 1.0
})
let alert = UIAlertController(title: "Error", message: "\(error!)", preferredStyle: .Alert)
let alertAction = UIAlertAction(title: "Ok", style: .Cancel, handler: nil)
alert.addAction(alertAction)
self.presentViewController(alert, animated: true, completion: nil)
} else {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.imageActInd.stopAnimating()
self.image.alpha = 1.0
self.image.image = image
})
}
})
}
我不知道为什么这些数组被重置为空。我做错了什么?
答案 0 :(得分:1)
[这假设所有这些代码都在单个UIViewController
实例中运行。]
我认为您误解了UIViewController
生命周期的某些方面。在你的应用程序的启动过程中,viewDidLoad()
仅被调用一次,当时没有任何UI可见,没有任何组件布局,也没有定时器被给予机会开始。除非您此时从持久性存储(磁盘;数据库;等)中加载数据,否则您的数组将具有从重写构造函数(如果有的话)设置的值,否则将具有从中设置的值他们的类级声明初始化器。
你什么时候开始NSTimer
,你什么时候开始关注它?
您使用println()
或调试断点来教自己真实的事件序列吗?
我还要补充一点,你在主线程上做了一些UI代码,这是一个很大的禁忌。警报的显示必须遵循与非错误UI路径相同的dispatch
模式。