0

我有一个集合视图,其中一些单元格代表一个联系人(他们的数据有一个电话号码和姓名),我正在尝试将联系人添加到 iPhone 联系人中。我从CollectionViewCell导航控制器内部的一个名为“添加联系人”的按钮创建了一个 segue,并将其标识符设置为“ADD_CONTACT”。
在情节提要中,我的 segue 有一个没有根视图控制器的导航控制器。在prepareToSegue委托我的视图控制器中,UICollectionView我编写了以下代码:

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

4

1 回答 1

1

最终,我使用闭包以不同的方法解决了这个问题。

在我的UICollectionViewCell 我添加了这个变量:

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

现在,在同一个单元格中的按钮操作上,我有这个功能:

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

哪个调用函数。

CollectionView索引路径的项目的 in 单元格中,我将闭包设置如下:

                    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)
                            }
                        }
                    }

这非常有效。我了解到,要从单元格导航,最好使用闭包。

于 2017-08-02T14:37:18.857 回答