2015-01-12 5 views
0

두 개의보기 컨트롤러 FirstViewControllerSecondViewController이 있습니다. SecondViewController은 정상적인보기 컨트롤러이며 UITableView입니다.스위프트 : UITableView 데이터로드 중

FirstViewController에서 나는 두 번째로 나타납니다. 내가 그것을 호출하면 대리인을 통해 데이터를 전달하려고합니다. 다음 대리인 프로토콜이다

protocol CalculationDelegate{ 
    func calculations(calculation: [Double]) 
} 

를 기본적으로는 두 배의 어레이를 통과한다. FirstViewController에서 A 버튼을 클릭하면 전환 준비를하고 대표를 설정하기 위해 prepareForSegue을 사용합니다.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    var secondVC = SecondViewController() 
    secondVC = segue.destinationViewController as SecondViewController 

    // Set up the delegation, so data is passed to SecondViewController 
    self.delegate = SecondViewController() 
    self.delegate?.calculations(wage.allCalculations) 
} 

이제 SecondViewController에 나는 내가 단지 UITableView에로드 할 통과 한 데이터에 액세스 할 수 있습니다. 당신은 내가 대의원 방법에 self.calculations = calculation를 할당하려고 볼 수 있듯이 여기 SecondViewController

class SecondViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, CalculationDelegate { 

    @IBOutlet 
    var tableView: UITableView! 
    var calculations: [Double] = [] 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell") 
    } 

    // Conform to CalculationsDelegate by implementing this method 
    func calculations(calculation: [Double]) { 
     self.calculations = calculation 

    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
    } 

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return self.calculations.count 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell 

     cell.textLabel?.text = NSString(format: "%.2f", self.calculations[indexPath.row]) 
     return cell 
    } 

} 

의 전체 클래스 선언이다. 그런 다음 데이터를 사용하여 UITableView을로드하면 데이터가로드되지 않습니다. 오류가 체크되어 있고 데이터가 대리자에서 전달되고 있습니다. 어떤 도움이라도 대단히 감사 할 것입니다.

답변

3

SecondViewController()은 실제로 뷰 컨트롤러의 새 인스턴스를 생성합니다. 코드를 살펴 봅니다.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    var secondVC = SecondViewController() // Line 1 
    secondVC = segue.destinationViewController as SecondViewController() // Line 2 

    // Set up the delegation, so data is passed to SecondViewController() 
    self.delegate = SecondViewController() // Line 3 
    self.delegate?.calculations(wage.allCalculations) 
} 

Line1을 살펴보십시오. secondVC에있는 SecondViewController의 새 인스턴스를 만들지는 않습니다.

라인 2 -이 라인은 세그먼트의 destinationViewController을 가져와야합니다.

라인 3 =이 라인은 필요하지 않은 다른 인스턴스를 다시 생성합니다. 이 인스턴스에 calculations array을 설정하고 있습니다. segue는 다른 인스턴스에서 작동합니다. 그래서 두 번째 뷰 컨트롤러 내부에서 배열을 가져 오지 못합니다.

다음 코드는 작동합니다.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    var secondVC = segue.destinationViewController as SecondViewController() 

    secondVC.calculations(wage.allCalculations) 
} 

계산 배열을 설정하는 데 왜 대리인과 프로토콜이 필요한지 알지 못합니다. SecondContController에서 calculations 함수를 정의하는 것만으로 충분합니다.

+0

네, 설명이 완벽하게 작동했습니다. –