[英]How do I display contacts when UITextField is clicked?
I have tried multiple times to create a UITextField
that when clicked should show the contacts available on the device and retrieve the phone number and display it in the textfield. 我已经尝试过多次创建一个
UITextField
,当单击该UITextField
时应显示设备上可用的联系人,并检索电话号码并将其显示在文本字段中。 However I have been unable to do that. 但是我一直无法做到这一点。 The best that I could do is to use a button to receive and display the number on a textfield.
我能做的最好的就是使用一个按钮来接收数字并将其显示在文本字段上。 This works!
这可行! How do I do the same for when the
UITextField
is clicked? 单击
UITextField
时该如何做?
I'm running it on Xcode 10 我在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 UITextField
is clicked, the contacts will appear and when one contact is selected, the number should appear in the textfield 我想要这样,以便在单击
UITextField
时出现联系人,并且在选择一个联系人时,该号码应出现在文本字段中
You should use textFieldShouldBeginEditing
method. 您应该使用
textFieldShouldBeginEditing
方法。 Open the contacts controller in this method and return false
, no need to add a gesture recogniser. 使用此方法打开通讯录控制器,然后返回
false
,无需添加手势识别器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.