2016-08-11 14 views
2

連絡先フレームワークを使用してユーザーの連絡先の名前、番号、電子メール、連絡先イメージを取得すると、データが取得されます。私が今表示できるのは、テーブルビューのキーの1つだけです。名前や電子メールのように、名前をクリックさせてから、他のビューコントローラーとセグをして残りのプロパティを表示したいアップ。私は他のviewControllerにそれを表示するのに問題があります。私はcontactsをフェッチするために使用していたコードを動作していないようでした、prepareforsegue機能でGET接点方式を使用してみましたが、私は私のコメントで言ったように、別のViewControllerで連絡先情報をセグを表示

func getContacts() { 
    let store = CNContactStore() 

    if CNContactStore.authorizationStatusForEntityType(.Contacts) == .NotDetermined { 
     store.requestAccessForEntityType(.Contacts, completionHandler: { (authorized: Bool, error: NSError?) -> Void in 
      if authorized { 
       self.retrieveContactsWithStore(store) 
      } 
     }) 
    } else if CNContactStore.authorizationStatusForEntityType(.Contacts) == .Authorized { 
     self.retrieveContactsWithStore(store) 
    } 
    tableView.reloadData() 
} 


func retrieveContactsWithStore(store: CNContactStore) { 
    do { 
     let groups = try store.groupsMatchingPredicate(nil) 
     // let predicate = CNContact.predicateForContactsInGroupWithIdentifier(groups[0].identifier) 
     let containerId = CNContactStore().defaultContainerIdentifier() 
     let predicate: NSPredicate = CNContact.predicateForContactsInContainerWithIdentifier(containerId) 
     let keysToFetch = [CNContactFormatter.descriptorForRequiredKeysForStyle(.FullName), CNContactEmailAddressesKey, CNContactPhoneNumbersKey,CNContactImageDataAvailableKey,CNContactThumbnailImageDataKey] 
     print(CNContactEmailAddressesKey.capitalizedString) 
     let contacts = try store.unifiedContactsMatchingPredicate(predicate, keysToFetch: keysToFetch) 
     self.objects = contacts 
     dispatch_async(dispatch_get_main_queue(), {() -> Void in 
      self.tableView.reloadData() 
     }) 
    } catch { 
     print(error) 
    } 
} 

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    if let identifier = segue.identifier { 
     if identifier == "DetailContact" { 

     } 
    } 






} 
+1

どこ 'prepareForSegue'方法はありますか? – Harsh

+0

他のView Controllerでプロパティを追加し、選択した連絡先をprepareToSegueに渡すことができます。連絡先を再度取得する必要はありません。 – Harsh

+1

が追加されました!今そこに – nkz

答えて

1

であるあなたの他のコントローラに新しい連絡先オブジェクトを作成します。たとえば:yourContactObject、その後、次のようにセグエの準備をして、それにいくつかの値を設定します。

var selectedContact;// Your contact which you will set in the did select of the other controller 

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    if let identifier = segue.identifier { 
     if identifier == "DetailContact" { 
      let controller = segue.destinationViewController 
      controller.yourContactObject = selectedContact. 
     } 
    } 
} 


func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    selectedContact = objects[indexPath.row] 
} 
1

代わりprepareForSegueでクエリを再やろうとしているのは、あなたの他のビューコントローラのプロパティを作成し、var contact: CNContactStore!を言うと、テーブルビューでどの行が選択されているかを観察します。だから、didSelectRowAtIndexPathで次の操作を行うことができます...

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    let contactSelected = yourDataModel[indexPath.row] //now you know which contact was selected 
    self.performSegueWithIdentifier("DetailContact", sender: contactSelected) 
    /**you can pass the selected contact as the sender argument, 
     or set it as a class-level variable, 
     or use a delegate or notification, all would work */ 
} 

そしてあなたprepareForSegue方法では、次の操作を行うことができ...

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    if let identifier = segue.identifier { 
     if identifier == "DetailContact" { 
      let controller = segue.destinationViewController 
      controller.contact = sender as! CNContactStore 
     } 
    } 
} 
関連する問題