繁体   English   中英

单击UITextField时如何显示联系人?

[英]How do I display contacts when UITextField is clicked?

我已经尝试过多次创建一个UITextField ,当单击该UITextField时应显示设备上可用的联系人,并检索电话号码并将其显示在文本字段中。 但是我一直无法做到这一点。 我能做的最好的就是使用一个按钮来接收数字并将其显示在文本字段上。 这可行! 单击UITextField时该如何做?

我在Xcode 10上运行

private let contactPicker = CNContactPickerViewController()
    override func viewDidLoad() {
        super.viewDidLoad()
        configureTextFields()
        configureTapGesture()
        phonenumber.textContentType = .telephoneNumber

     }


    private func configureTextFields() {
        phonenumber.delegate = self

    }

    private func configureTapGesture(){
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(SelfTestTimer.handleTap))
        viewcontact.addGestureRecognizer(tapGesture)

    }

    @objc private func handleTap(){
        viewcontact.endEditing(true)

    }



    @IBAction func pbbbbb(_ sender: Any) {
        contactPicker.delegate = self
        self.present(contactPicker, animated: true, completion: nil)


    }

}
    extension SelfTestTimer: CNContactPickerDelegate {

        func contactPicker(_ picker: CNContactPickerViewController, didSelect contact: CNContact) {


            let phoneNumberCount = contact.phoneNumbers.count

            guard phoneNumberCount > 0 else {
                dismiss(animated: true)

                return
            }

            if phoneNumberCount == 1 {
                setNumberFromContact(contactNumber: contact.phoneNumbers[0].value.stringValue)

            }else{

                let alertController = UIAlertController(title: "Select one of the numbers", message: nil, preferredStyle: .alert)

                for i in 0...phoneNumberCount-1 {
                    let phoneAction = UIAlertAction(title: contact.phoneNumbers[i].value.stringValue, style: .default, handler: {
                        alert -> Void in
                        self.setNumberFromContact(contactNumber: contact.phoneNumbers[i].value.stringValue)
                    })
                    alertController.addAction(phoneAction)
                }
                let cancelAction = UIAlertAction(title: "Cancel", style: .destructive, handler: {
                    alert -> Void in

                })
                alertController.addAction(cancelAction)

                dismiss(animated: true)
                self.present(alertController, animated: true, completion: nil)
            }
        }

        func setNumberFromContact(contactNumber: String) {


            var contactNumber = contactNumber.replacingOccurrences(of: "-", with: "")
            contactNumber = contactNumber.replacingOccurrences(of: "(", with: "")
            contactNumber = contactNumber.replacingOccurrences(of: ")", with: "")
            guard contactNumber.count >= 10 else {
                dismiss(animated: true) {
                    self.presentAlert(alertTitle: "", alertMessage: "A maximum of 10 contacts allowed per session", lastAction: nil)
                }
                return
            }
            phonenumber.text = String(contactNumber.suffix(10))

        }

        func contactPickerDidCancel(_ picker: CNContactPickerViewController) {

        }




}

extension SelfTestTimer: UITextFieldDelegate {
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }


}

我想要这样,以便在单击UITextField时出现联系人,并且在选择一个联系人时,该号码应出现在文本字段中

您应该使用textFieldShouldBeginEditing方法。 使用此方法打开通讯录控制器,然后返回false ,无需添加手势识别器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM