我正在使用联系框架来获取用户联系人的姓名,号码,电子邮件和联系人图像,正在获取数据。我现在能够显示的只是表格视图中的一个键,比如姓名或电子邮件,我希望点击名称,然后转到其他属性显示的其他视图控制器起来。但是我在另一个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" {
}
}
}
答案 0 :(得分:1)
正如我在评论中所说,在其他控制器中创建一个新的联系对象。例如:yourContactObject
然后在准备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]
}
答案 1 :(得分: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
}
}
}