我正在使用Daniel Cohen Gindi的开源Charts
:https://github.com/danielgindi/Charts
我的屏幕上有一个饼图,它的图形如下:
//set pie chart data
graphView.data = PieChartData(dataSets: iPieChartDataSet)
此外,我在屏幕上显示一个按钮similarly to the accepted answer of this question。
“我想要的”
当用户按下屏幕上的UIButton
时,我想要显示一个弹出框。
“什么有效”
当我按下UIButton
时,将按预期创建并显示弹出窗口。此外,我可以创建弹出窗口,关闭它,然后重新创建它。
“什么行不通”
如果图形出现在屏幕上,则弹出窗口不会出现。我尝试在print
中使用UIButton
语句,看起来IBAction
永远不会被解雇。
其他信息
其他UI操作有效。我可以选择其他选项卡,我可以旋转图形。如果屏幕上的图形存在,似乎UIButton
以某种方式被关闭。
相关代码: 弹出窗口的创建方式:
@IBAction func informationButtonTapped(_ sender: UIButton) {
var popoverContent = (self.storyboard?.instantiateViewController(withIdentifier: segueIdentifiers.informationPopover))! as UIViewController
var nav = UINavigationController(rootViewController: popoverContent)
nav.modalPresentationStyle = UIModalPresentationStyle.popover
var popover = nav.popoverPresentationController
popoverContent.preferredContentSize = CGSize(width: 500,height: 600)
popover?.delegate = self
popover?.sourceView = self.view
popover?.sourceRect = CGRect(x: 0, y: 0, width: 100, height: 100)
self.present(nav, animated: true, completion: nil)
}
如何创建图表(尽可能缩短):
private func setChart(dataPoints: [String?], values: [Double]) {
var dataEntries: [PieChartDataEntry] = []
for i in 0..<dataPoints.count {
let dataEntry = PieChartDataEntry(value: values[i], label: dataPoints[i])
dataEntries.append(dataEntry)
}
let pieChartDataSet = PieChartDataSet(values: dataEntries, label: "")
var iPieChartDataSet: [IChartDataSet] = []
iPieChartDataSet.append(pieChartDataSet)
graphView.animate(xAxisDuration: 1.0, yAxisDuration: 1.0)
graphView.layer.borderWidth = 0.1
//set pie chart data
graphView.data = PieChartData(dataSets: iPieChartDataSet)
}
答案 0 :(得分:0)
对于我的应用程序,我以编程方式创建了按钮,到目前为止我没有遇到任何问题。继承我的按钮代码,看看它是否适合你。
var updateButton : UIButton = UIButton(type: .custom)
override func viewDidLoad() {
super.viewDidLoad()
setChart()
let buttonHeight = 60.0
let buttonWidth = 280.0
let xspacing = 45.0
let fontSize:CGFloat = 30
let buttonColor = UIColor.black
let textColor = UIColor.blue
updateButton.frame = CGRect(x: xspacing, y: 400, width: buttonWidth, height: buttonHeight)
updateButton.backgroundColor = buttonColor
updateButton.clipsToBounds = true
updateButton.setTitle("Update", for: .normal)
updateButton.titleLabel!.font = UIFont(name: "ScienceFair", size: fontSize)
updateButton.titleLabel!.textColor = UIColor.black
updateButton.addTarget(self, action: #selector(updateFunc), for: .touchUpInside)
view.addSubview(updateButton)
}
func updateFunc() {
UIView.animate(withDuration: 0.1, animations: { self.updateButton.transform = CGAffineTransform(scaleX: 0.9, y: 0.9) }, completion: { (finish: Bool) in UIView.animate(withDuration: 0.1, animations: { self.updateButton.transform = CGAffineTransform.identity }) })
//Present Popover
}
忽略已注释的#selector的stackoverflow格式。这不是评论。