0

I am using the open-sourced Charts by Daniel Cohen Gindi: https://github.com/danielgindi/Charts

I have a pie chart on my screen which graphs itself like this:

//set pie chart data
graphView.data = PieChartData(dataSets: iPieChartDataSet)

Additionally I have a button on the screen which displays a popover similarly to the accepted answer of this question.

"What I Want" When the user presses the UIButton on the screen, I want a popover to display.

"What works" When I press the UIButton, the popover is created and presented as expected. Additionally I can create the popover, close it, and recreate it.

"What doesn't work" If the graph is present on the screen, then the popover does not appear. I tried to using a print statement within the UIButton and it looks like the IBAction is never fired.

Other information Other UI actions work. I can tab to other tabs and I can swivel the graph around. It just seems that the UIButton is somehow getting turned off if the graph on the screen is present.

Relevant code: How the popover is being created:

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

How the graph is being created (shortened as much as possible):

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)
}
Community
  • 1
  • 1
Sami
  • 579
  • 5
  • 25

1 Answers1

0

For my app I created the buttons programmatically, and I've had no issues so far. Heres my button code, see if it works for you.

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
}

Ignore stackoverflow formatting for the commented #selector. Its not a comment.

Aditya Garg
  • 838
  • 6
  • 19