2

我使用联系人框架来获取用户联系人的姓名、号码、电子邮件和联系人图像,正在获取数据。我现在能够显示的只是表格视图中的一个键,例如姓名号码或电子邮件,我希望单击该名称,然后转到显示其余属性的另一个视图控制器向上。但我无法在另一个上显示它viewController。我已经尝试在prepareforsegue函数中使用 get contacts 方法,似乎没有工作我用来获取的代码contacts

 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" {

        }
    }






}
4

2 回答 2

1

正如我在评论中所说,在您的另一个控制器中创建一个新的联系人对象。例如:yourContactObject然后在prepare for segue中为其设置一些值,如下所示:

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]
}
于 2016-08-11T21:42:36.197 回答
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
        }
    }
}
于 2016-08-11T21:44:24.010 回答