【问题标题】:Selected text doesn't appear on the UITextField所选文本未出现在 UITextField 上
【发布时间】:2019-05-09 10:59:37
【问题描述】:

当我点击UITextField 时,会出现一个联系人列表,当点击联系人时,电话号码应该会出现在被点击的文本字段中。我目前有 3 个文本字段,每次选择联系人时,即使例如我选择了第二个文本字段,它也只会更新第一个文本字段。如何修复它以使电话号码出现在所选的相应文本字段中?

我使用的是 Xcode 10,我认为问题出在 func setNumberFromContact

    @IBOutlet weak var phonenumber: UITextField!
    @IBOutlet weak var phonenumber1: UITextField!
    @IBOutlet weak var phonenumber2: UITextField!


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


            let phoneNumberCount = contact.phoneNumbers.count

            guard phoneNumberCount > 0 else {
                dismiss(animated: true)
                //show pop up: "Selected contact does not have a number"
                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
    }


    func textFieldDidBeginEditing(_ textField: UITextField) {

        if textField.hasText{
            //dont do anything

        }else{

        contactPicker.delegate = self
        self.present(contactPicker, animated: true, completion: nil)
        }
        return
    }

【问题讨论】:

  • 向@RajeshKumarR 的回答添加更多信息:只有一个文本字段正在更新的原因是因为这一行:phonenumber.text = String(contactNumber.suffix(10))。您只更改 phonenumber 文本字段。他的解决方案应该可以正常工作。
  • 是的,它完全有效。非常感谢@Marcel 的输入

标签: ios swift uitextfield


【解决方案1】:

您的解决方案只更新一个文本字段的原因是因为您只更新该文本字段的文本。在这一行phonenumber.text = String(contactNumber.suffix(10)) 中,您只更改phonenumber 的文本。一个好的解决方案如下:

创建一个临时 UITextField 来存储选定的文本字段引用

@IBOutlet weak var phonenumber: UITextField!
@IBOutlet weak var phonenumber1: UITextField!
@IBOutlet weak var phonenumber2: UITextField!
var currentTextField: UITextField?

并在文本字段委托方法中使用该文本字段

extension SelfTestTimer: UITextFieldDelegate {
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        currentTextField = nil
        textField.resignFirstResponder()
        return true
    }
    func textFieldDidBeginEditing(_ textField: UITextField) {
        if textField.hasText{
            //dont do anything
        }else{
            currentTextField = textField
            contactPicker.delegate = self
            self.present(contactPicker, animated: true, completion: nil)
        }
        return
    }
}

在该文本字段中分配选定的联系人号码

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
    }
    currentTextField?.text = String(contactNumber.suffix(10))
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-21
    • 2021-03-19
    • 2012-01-26
    • 2010-11-19
    • 2015-01-19
    • 1970-01-01
    相关资源
    最近更新 更多