首页 > 解决方案 > 单击 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
    }


}

I want it so that when the UITextFieldis clicked, the contacts will appear and when one contact is selected, the number should appear in the textfield

标签: iosswiftxcodeuitextfield

解决方案


你应该使用textFieldShouldBeginEditing方法。在此方法中打开联系人控制器并返回false,无需添加手势识别器。


推荐阅读