2016-07-31 8 views
0

折れ線グラフと棒グラフの2つの別々のxibファイルを作成し、それを私のシーンのビューにリンクしました。私はペン先をデータの種類に応じて割り当てました。私は私のViewControllerからチャートを描画するために呼び出す必要がある私のUIViewクラスの関数setChart()を持っています。ViewControllerのUIViewメソッドを呼び出す

私のViewControllerから関数setChart()を呼び出すにはどうすればよいですか?私のコードは動作しません

のUIView:

class BarChartDashboard: UIView { 

@IBOutlet weak var barChartView: BarChartView! 

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

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

    ..... 
} 


} 

のViewController:あなたのDetailDashboardViewControllerあなたがUIViewの種類としてビューのインスタンスを作ったカスタムUIViewクラスが命名されている間に

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を作成し、あなたのViewControllerクラスは以下のようになります

XIBから接続viewoutletsubViewとしてこのチャートを追加します。

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

BarChartDashboardなので、メソッドsetChart()を呼び出すには、クラスインスペクタのビューのクラスをBarChartDashboardとに設定する必要がありますインスタンス変数は、次のように変更する必要があります。

@IBOutlet weak var chartView: BarChartDashboard! 

だから今、インスタンス変数は、あなたのDetailDashboardViewControllerから呼び出すことができsetChart()BarChartDashboardとなりまし方法の種類です。あなたの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]) 
} 
関連する問題