只能选择一个联系人,如何使用ContactUI进行多选?

时间:2017-01-13 19:47:18

标签: ios swift

希望:我正在尝试在ContactsUI中选择多个联系人:How I want it (dont mind blue boxes)

CURRENT:我无法找到一种方法来获取表格视图单元格中的多个选择按钮How it looks, only single selection

从下面的代码中我可以看到我正在使用contactPickerView所以我不知道我做错了什么:/

import UIKit
import ContactsUI
import Contacts


class FindContactsVC: UIViewController, CNContactPickerDelegate {

    //button to open contactUI
    @IBAction func contactsButton(_ sender: Any) {
        let cnPicker = CNContactPickerViewController()
        cnPicker.delegate = self    
        self.present(cnPicker, animated: true, completion: nil)  
    }

    //Works, but only allows me to select one at a time
    func contactPicker(_ picker: CNContactPickerViewController, didSelect contact: CNContact) {
        print(contact.phoneNumbers)
        print("WYASDFY")
    }

    //This function never runs :/
    func contactPicker(picker: CNContactPickerViewController, didSelectContacts contacts: [CNContact]){
       print(contacts)
        print("plz work!")
        contacts.forEach { contact in
            for number in contact.phoneNumbers {
                let phoneNumber = number.value 
                print("number is = \(phoneNumber)")
            }
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()       
    }
}

谢谢^。^

1 个答案:

答案 0 :(得分:3)

我还没有检查过代码的其他部分,但至少,你需要正确实现第二个委托方法:

func contactPicker(picker: CNContactPickerViewController, didSelectContacts contacts: [CNContact]){

func contactPicker(_ picker: CNContactPickerViewController, didSelect contacts: [CNContact]) {