2016-06-05 3 views
0

나는 tableview와 viewcontroller 사이에 데이터를 전달하려고합니다. 여기서는 반복하지 않기 때문에 코드를 통해 프로그래밍 방식으로 뷰 컨트롤러를 만들었습니다. 스토리 보드에 참조 및 존재 여부 (스토리 보드에 ViewController가 없음)가 없었습니다. 모든 것이 잘 작동하고 그래픽 전환 또한 데이터 전환이됩니다. 그래픽 전환 전에 "didSelectRowAtIndexPath"에서 데이터 전환을 수행했습니다. TableViewController스토리 보드 참조없이 컨트롤러간에 데이터 전달

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 

    var arrayToUse:NSArray? 
    var tableInUse:UITableView? 
    let dc = DetailsViewController() 

    // Retrive the correct data here 

    let indexPath = tableInUse?.indexPathForSelectedRow 
    dc.purchase = arrayToUse?.objectAtIndex((indexPath?.row)!) as! Purchase 
    let temp = arrayToUse?.objectAtIndex((indexPath?.row)!) as! Purchase 
    print(temp.title) 

    self.navigationController!.pushViewController(DetailsViewController(), animated: true) 
} 

에서의 ViewController

var purchase:Purchase? 

에서

은 여기와 다른 사이트에있는 다른 솔루션을 보았다,하지만 난 항상 "SEGUE"또는 "펜촉을"고민 해결책을 발견했다. 내가 뭘 잘못하고있어?

나는 분명히했으면 좋겠다. 모든 종류의 도움이나 정보는 정말 환영합니다.

감사

답변

4

문제는 당신이 DetailsViewController의 인스턴스를 생성하고 여기에 데이터를 설정하지만, 다른 DetailsViewController 개체를 추진하는 것입니다. 새 인스턴스를 만드는 대신 이전에 만든 dc 개체를 사용해야합니다.

self.navigationController!.pushViewController(DetailsViewController(), animated: true) 

사용한다 : 대신

self.navigationController!.pushViewController(dc, animated: true) 
+0

감사합니다. 너무 단순했습니다. 때때로 Ti는 스스로 위로 올라갑니다. 다시 고마워. – Swalsz

+0

@ 스왈츠 : 천만에. 해피 코딩 !!! –

관련 문제