0

I am trying to create a bar graph class in swift but having difficulty setting up a delegate....heres what I have so far...

BarChart.swift:

import Foundation
import UIKit

@objc protocol BarChartDelegate {

    optional func barChart(colorForBarAtIndex index: Int) -> UIColor
}

class BarChart : BarChartDelegate {

    let data : [NSDecimalNumber]
    let container = UIView()

    var barWidth : CGFloat = 100
    var barSpacing : CGFloat = 10

    var delegate : BarChartDelegate?

    init(data: [NSDecimalNumber], frame: CGRect) {

        self.data = data
        self.container.frame = frame

        drawGraph()

    }

    func drawGraph() {

        var i = 0

        for item in self.data {

            var bar = UIView()  

            let xPos = CGFloat(i)*self.barWidth

            bar.frame = CGRectMake(xPos, 0, self.barWidth, 100)

            if let del = delegate? {

                bar.frame 
                println(del.barChart!(colorForBarAtIndex: i))

                bar.backgroundColor = del.barChart!(colorForBarAtIndex: i)

            }
            else {
                println("nope!")
            }

            self.container.addSubview(bar)

            i++

        }

    }

}

ViewController.swift

class ViewController: UIViewController, BarChartDelegate {

    var colors = [
        UIColor.greenColor(),
        UIColor.blueColor(),
        UIColor.redColor()
    ]

    override func viewDidLoad() {

        super.viewDidLoad()

        var barChart = BarChart(data: [NSDecimalNumber(double: 100.00), NSDecimalNumber(double: 200.00), NSDecimalNumber(double: 300.00)], frame: CGRectMake(0, 0, 400.00, 100.00))

        self.view.addSubview(barChart.container)

    }

    func barChart(colorForBarAtIndex index: Int) -> UIColor { // this is not running?

        return self.colors[index]

    }


}

the delegate method in my ViewController.swift file is not being run and I just get "nope!" printed to the console 3 times... which is a result of the delegate optional finding nil when unwrapping?

Is there something I am missing here?

Any help would be appreciated!

Thanks, Dave

1 Answer 1

1

First off, it rarely makes sense for a BarChart to also be a BarChartDelegate. You don't delegate things to yourself!

Second, as far as I can tell you're not actually setting the ViewController as the delegate of the BarChart. Simply adopting the BarChartDelegate protocol is not enough to do that; you need to set it explicitly.

So, for example, you might want to do this after you create the BarChart:

var barChart = ...
barChart.delegate = self

Alternatively, if the delegate is vital for your chart, you might want to change the constructor to accept a delegate as an argument.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the quick reply sapi! Adding a delegate argument to the constructor worked for me Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.