我尝试使用 https://github.com/CoreCharts/CoreCharts 制作条形图我有一个按钮,它使用 textField 调用 UIAlertController。 单击提交按钮后,新数据将添加到数组中。我需要图表才能重新启动。
@IBAction func newWeightButton(_ sender: Any) {
//updateGraph()
let alert = UIAlertController(title: "Enter your new weight", message: nil, preferredStyle: .alert)
alert.addTextField()
let submitAction = UIAlertAction(title: "Add", style: .default) { [unowned alert] _ in
let textField = alert.textFields![0]
textField.keyboardType = UIKeyboardType.numberPad
textField.becomeFirstResponder()
let answer = Double(textField.text!)
self.weightArr.append(answer!)
print(self.weightArr)
self.loadCoreChartData()
}
alert.addAction(submitAction)
present(alert, animated: true)
}
func loadCoreChartData() -> [CoreChartEntry] {
return getWeightList()
}
func getWeightList()->[CoreChartEntry] {
var allResults = [CoreChartEntry]()
//let days = ["Mon","Tue","Wed","Thur","Fri"]
let plateNumber = [80,75,90,88,84]
var count = self.weightArr.count
for index in 0..<count {
let newEntry = CoreChartEntry(id: "\(self.weightArr[index])",
barTitle: "Day",
barHeight: Double(self.weightArr[index]),
barColor: UIColor(red: 0, green: 122/255, blue: 1, alpha: 1))
allResults.append(newEntry)
}
最佳答案
对于 CoreCharts,在图表上调用 reload()
。
通常,在 iOS 中,设置 UIView
的属性就足以让它更新,但是对于使用某种类型的数据源(如表格、 Collection View 和核心图表)的 View ,通常有某种 reload...() 函数来告诉 View 数据源已更改。
关于swift - 关闭 UIAlertController 后如何重新加载 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56619879/