Nithin Reddy Gaddam
Nithin Reddy Gaddam

Reputation: 65

Call UIView method from ViewController

I created a two separate xib files for a line graph and a bar chart and linked it to a view in my scene. I assigned the nib depending on the type of data. I have a function setChart() in my UIView class which I need to call from my ViewController to draw the charts.

How do I call the function setChart() from my ViewController? My code doesn't work

UIView:

class BarChartDashboard: UIView {

@IBOutlet weak var barChartView: BarChartView!

override func awakeFromNib() {
    .....
}

func setChart(dataPoints: [String], values: [Double]) {

    .....
}


}

ViewController:

class DetailDashboardViewController: UIViewController {

@IBOutlet weak var chartView: UIView!

override func viewDidLoad() {
    super.viewDidLoad()

    runQueries(2)

    if index != 3{
        chartView = NSBundle.mainBundle().loadNibNamed("BarChartDashboard", owner: self, options: nil)[0] as? UIView
    }
    else{
        chartView = NSBundle.mainBundle().loadNibNamed("LineChartDashboard", owner: self, options: nil)[0] as? UIView
    }
}
}

private extension DetailDashboardViewController {

func runQueries(period: Int) {

    self.query(false, periodIndex: period){ (success) -> Void in
        if success {
            // do second task if success
            dispatch_async(dispatch_get_main_queue(), {
                if self.index != 3{
                    (self.chartView as! BarChartDashboard).setChart(self.yAxis, values: self.xAxis)
                }
                else{
                    (self.chartView as! LineChartDashboard).setChartData(self.yAxis, yAxisMax: self.xAxisMax, yAxisMin : self.xAxisMin)
                }
            })
        }
    }
}


func query(min: Bool, periodIndex: Int, completion: (success: Bool) -> Void) {
    .......
}

}

Upvotes: 0

Views: 1597

Answers (2)

Mohamad Sheikh
Mohamad Sheikh

Reputation: 283

In your DetailDashboardViewController you have made an instance of your view as type of UIView while your custom UIView class is named BarChartDashboard, so in order to be able to call method setChart() you should set the class of your view in class inspector to BarChartDashboard as well as in your DetailDashboardViewController instance variable should be changed as follow:

@IBOutlet weak var chartView: BarChartDashboard!

So now the instance variable is type of BarChartDashboard and now method setChart() can be called from your DetailDashboardViewController.

In your DetailDashboardViewController

@IBOutlet weak var chartView: BarChartDashboard!
override func viewDidLoad() {
     super.viewDidLoad()

     runQueries(2)

     if index != 3{
        chartView = NSBundle.mainBundle().loadNibNamed("BarChartDashboard", owner: self, options: nil)[0] as? UIView
     }
     else{
        chartView = NSBundle.mainBundle().loadNibNamed("LineChartDashboard", owner: self, options: nil)[0] as? UIView
     }
     chartView.setChart(dataPoints: ["hello","world"], values: [20.0,21.5])
}

Upvotes: 1

Mohammed Shakeer
Mohammed Shakeer

Reputation: 1504

Create a new variable chart in your ViewController and add this chart as subView to the view outlet connected from xib

Your ViewController class would look like below:

class DetailDashboardViewController: UIViewController {

@IBOutlet weak var chartView: UIView!
var chart: UIView!

override func viewDidLoad() {
    super.viewDidLoad()

        if index != 3{
            chart = NSBundle.mainBundle().loadNibNamed("BarChartDashboard", owner: self, options: nil)[0] as? BarChartDashboard
     }
        else{
            chart = NSBundle.mainBundle().loadNibNamed("LineChartDashboard", owner: self, options: nil)[0] as? LineChartDashboard
        }

    self.chartView.addSubView(chart)
    self.chart.frame = self.chartView.bounds
    runQueries(2)
    }

private extension DetailDashboardViewController {

func runQueries(period: Int) {

    self.query(false, periodIndex: period){ (success) -> Void in
        if success {
            // do second task if success
            dispatch_async(dispatch_get_main_queue(), {
                if self.index != 3{
                    (self.chart as! BarChartDashboard).setChart(self.yAxis, values: self.xAxis)
                }
                else{
                    (self.chart as! LineChartDashboard).setChartData(self.yAxis, yAxisMax: self.xAxisMax, yAxisMin : self.xAxisMin)
                }
            })
        }
    }
}

Upvotes: 1

Related Questions