Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 单击UITextField时如何显示联系人?_Ios_Swift_Xcode_Uitextfield - Fatal编程技术网

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

Ios 单击UITextField时如何显示联系人?,ios,swift,xcode,uitextfield,Ios,Swift,Xcode,Uitextfield,我曾多次尝试创建一个UITextField,单击该字段时应显示设备上可用的联系人,检索电话号码并将其显示在textfield中。然而,我一直无法做到这一点。我所能做的最好的事情就是使用一个按钮接收并在文本字段上显示数字。这管用!单击UITextField时如何执行相同操作 我在Xcode 10上运行它 private let contactPicker = CNContactPickerViewController() override func viewDidLoad() {

我曾多次尝试创建一个
UITextField
,单击该字段时应显示设备上可用的联系人,检索电话号码并将其显示在textfield中。然而,我一直无法做到这一点。我所能做的最好的事情就是使用一个按钮接收并在文本字段上显示数字。这管用!单击
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
时,联系人会出现,当选择一个联系人时,号码会出现在textfield

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

您应使用
textfield应开始编辑方法。使用此方法打开contacts controller并返回
false
,无需添加手势识别器