2016-07-31 81 views
0

我爲線圖和條形圖創建了兩個單獨的xib文件,並將其鏈接到場景中的視圖。我根據數據類型分配了筆尖。我在我的UIView類中有一個函數setChart(),我需要從我的ViewController調用以繪製圖表。從ViewController調用UIView方法

如何從我的ViewController調用函數setChart()?我的代碼不能正常工作

的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) { 
    ....... 
} 

} 

回答

1

創建您的ViewController一個新的變量chart並添加此表作爲subViewviewoutlet從廈門國際銀行

你的ViewController類看起來像下面連接:

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

在你DetailDashboardViewController你們已經做出了視圖類型的UIView的實例,而您的自定義UIView類被命名爲BarChartDashboard,所以爲了能夠調用方法setChart(),您應該將您在班級督察中的視圖級別設置爲BarChartDashboard以及您的DetailDashboardViewController實例變量應更改如下:

@IBOutlet weak var chartView: BarChartDashboard! 

所以,現在的實例變量的類型BarChartDashboard,現在方法setChart()可以從DetailDashboardViewController被調用。

在你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]) 
} 
相關問題