将导航控制器视图控制器设置为 CNContactViewController 黑屏
setup navigation controller View Controller as a CNContactViewController black screen
我有一个集合视图,其中一些单元格代表联系人(它们的数据有 phone 号码和姓名),我正在尝试将联系人添加到 iPhone 联系人。我创建了一个从 CollectionViewCell
内名为 "add contact" 的按钮到导航控制器的转场,并将其标识符设置为 "ADD_CONTACT".
在故事板中,我的 segue 有一个没有根视图控制器的导航控制器。
在委托我的 UICollectionView
的视图控制器的 prepareToSegue
中,我写了这段代码:
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
最终我使用闭包以不同的方法解决了这个问题。
在我的UICollectionViewCell
我添加了这个变量:
var closureForContact: (()->())? = nil
现在我在同一个单元格中的按钮操作上有这个功能:
@IBAction func addContactTapped(_ sender: UIButton) {
if closureForContact != nil{
closureForContact!()
}
}
调用函数。
在我的 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)
}
}
}
这非常有效。我了解到,对于从单元格导航,最好使用闭包。
我有一个集合视图,其中一些单元格代表联系人(它们的数据有 phone 号码和姓名),我正在尝试将联系人添加到 iPhone 联系人。我创建了一个从 CollectionViewCell
内名为 "add contact" 的按钮到导航控制器的转场,并将其标识符设置为 "ADD_CONTACT".
在故事板中,我的 segue 有一个没有根视图控制器的导航控制器。
在委托我的 UICollectionView
的视图控制器的 prepareToSegue
中,我写了这段代码:
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
最终我使用闭包以不同的方法解决了这个问题。
在我的UICollectionViewCell
我添加了这个变量:
var closureForContact: (()->())? = nil
现在我在同一个单元格中的按钮操作上有这个功能:
@IBAction func addContactTapped(_ sender: UIButton) {
if closureForContact != nil{
closureForContact!()
}
}
调用函数。
在我的 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)
}
}
}
这非常有效。我了解到,对于从单元格导航,最好使用闭包。