2016-09-22 2 views
0

세부 정보를 사용하여 탐색을 이해하는 데 도움이 필요하며 여기 또는 Apple 설명서에서 적합한 대답을 찾을 수 없습니다.하나의보기 컨트롤러에서 동일한 데이터를 전달하는 두 개의 단편

나는 명상 오디오 플레이어를 만들고 있습니다.

다른 명상의 테이블을 보여주는 주 'ViewController'가 있습니다.

나는 명상의 오디오 파일을 재생하는 'SecondViewController'를 가지고 있습니다.

나는 무료 인물과 유료 인물이 있습니다.

NSUserDefaults를 사용하여 명상을 구입했는지 여부에 대한 정보가 포함 된 사전을 저장하고 있습니다.

사용자가 'ViewController'의 표 셀을 클릭하면 구입했는지 여부를 확인해야합니다.

구매 한 경우 'SecondViewController'를 표시하고 두 변수를 전달하고 싶습니다. 구매하지 않은 경우 'PurchaseViewController'를 표시하고 동일한 두 변수를 전달해야합니다. 여기

내가 사용하고있는 코드입니다 :

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 

    var index = tableView.indexPathForSelectedRow?.row 

    if pDictionary[purchasedArray[index!]]=="Purchased"||pDictionary[purchasedArray[index!]]=="Included"{ 
     let medSegueIdentifier = "sendDataSegue" 
      if segue.identifier == medSegueIdentifier { 
       index = tableView.indexPathForSelectedRow?.row} 
     let destination = segue.destinationViewController as? SecondViewController 
     destination!.medName = names[index!] 
     destination!.purchased = true 

    } else if pDictionary[purchasedArray[index!]]=="NotPurchased"{ 
     let medSegueIdentifier = "purchaseSegueIdentifier" 
      if segue.identifier == medSegueIdentifier {     
       index = tableView.indexPathForSelectedRow?.row} 
     let destination = segue.destinationViewController as? PurchaseViewController 
     destination!.medName = names[index!] 
     destination!.purchased = false 
    } 

(구입 한 경우) if 문의 첫 번째 지점이 완벽하게 작동하지만 (구입하지 않은 경우) 오류를주는 두 번째 "예기치 않게을 풀기 동안 전무 발견 선택적 값 ".

destination!.medNamenil으로 표시되지만 두 번째 분기에만 표시됩니다. 이것은 내가 이해할 수없는 부분이다.

여기에 a screenshot of my storyboard입니다.

+0

당신은 확실히 당신은 스토리 보드에서 ** ** purchaseSegue 추가? 관계를 생성하는 동안 그렇지 않다면 ** ** "purchaseSegue"** ViewController **와 ** PurchaseViewController **의 관계 사이 ** –

+0

예 ... 'SecondViewController'에 대한 segue에는 'sendDataSegue'의 식별자가 있고 'segue' PurchaseViewController '에'purchaseSegueIdentifier 'ID가 있습니다. –

답변

0

nil 인 유일한 방법은 캐스팅이 작동하지 않는다는 것입니다. 그래서 segue.destinationViewControllerPurchaseViewController

의 인스턴스가 아닌 것으로 생각합니다. 스토리 보드를 다시 열어 확인하려면 viewcontroller를 선택하고 속성 검사기를 엽니 다. 클래스 이름을 확인하십시오. PurchaseViewController에 연결하는 동안

enter image description here

+0

나는 그것을 확인하는 방법이 100 % 확실하지는 않지만 ... 'PurchaseViewcontroller '에 대한 스토리 보드에는'PRESENTING SEGUES '섹션에 표시된 관계가 있습니다. show -> ViewController manual –

+0

나는 내 대답을 더 자세히 편집했다. 해명 해 주셔서 감사합니다. – IgnazioC

+0

. 내 'SecondViewController'는 클래스 하고 'PurchaseViewController'로 클래스와 같은 'SecondViewController', '의 ViewController'의 ViewController '를 가지고있다하면 클래스에'PurchaseViewController '을 가지고 –

관련 문제