2017-07-31 18 views
0

連絡先を表す一部のセル(データに電話番号と名前があります)のコレクションビューがあり、その連絡先をiPhoneの連絡先に追加しようとしています。私は、ナビゲーションコントローラにCollectionViewCellの内部にある「連絡先を追加」というボタンからセグを作成し、その識別子を「ADD_CONTACT」と設定しました。
ストーリーボードでは、私のsegueにはルートコントローラーのないナビゲーションコントローラーがあります。 は、デリゲートが私のUICollectionViewビューコントローラのprepareToSegueに私はこのコードを書いた:セットアップナビゲーションコントローラCNContactViewControllerの黒い画面としてコントローラを表示

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 

    if segue.identifier == ADD_CONTACT { 
     let dest = segue.destination as! UINavigationController 
     if let cell = sender as? SBInstructionCell { 
      if cell.isContact { 
       let newContact = CNMutableContact() 

       if let phone = cell.instructionBean?.contactAttachment?.phoneNumber{ 
        newContact.phoneNumbers.append(CNLabeledValue(label: "home", value: CNPhoneNumber(stringValue: phone))) 
       } 
       if let name = cell.instructionBean?.contactAttachment?.contactName { 
        newContact.givenName.append(name) 
       } 
       let contactVC = CNContactViewController(forNewContact: newContact) 
       contactVC.contactStore = CNContactStore() 
       contactVC.delegate = self 
       dest.setViewControllers([contactVC], animated: false) 

      } 
     } 
    } 
} 

これは黒い画面になります。 これをどのように修正できますか?私は見たいと思っていますCNContactViewController

答えて

1

最終的に私はこれをClosuresを使って別の方法で解決しました。関数を呼び出す

@IBAction func addContactTapped(_ sender: UIButton) { 
    if closureForContact != nil{ 
     closureForContact!() 
    } 
    } 

:私はこのFUNCを持っている同じセル内の私のボタンのアクションに今

var closureForContact: (()->())? = nil 

:私のUICollectionViewCell

私はこのVARを追加しました。項目のセル内の私のCollectionView

インデックスパスで、私はこのような閉鎖を設定します。

    cell.closureForContact = { 

         if cell.isContact { 
          let newContact = CNMutableContact() 

          if let phone = cell.instructionBean?.contactAttachment?.phoneNumber{ 
           newContact.phoneNumbers.append(CNLabeledValue(label: "home", value: CNPhoneNumber(stringValue: phone))) 
          } 
          if let name = cell.instructionBean?.contactAttachment?.contactName { 
           newContact.givenName.append(name) 
          } 
          let contactVC = CNContactViewController(forNewContact: newContact) 
          contactVC.contactStore = CNContactStore() 

          contactVC.delegate = self 
          contactVC.allowsEditing = true 
          contactVC.allowsActions = true 

          if let nav = self.navigationController { 
           nav.navigationBar.isTranslucent = false 

           nav.pushViewController(contactVC, animated: true) 
          } 
         } 
        } 

これは完全に働きました。私は、セルからナビゲートするために、クロージャを使用することをお勧めします。

関連する問題