在我的应用程序中,我有一个按钮,其标题为“ IN +91”,点击该按钮后,将根据选择的国家/地区代码进行更改,将会弹出一个弹出窗口,并且无论您选择哪个国家/地区,都会显示该国家/地区的代码名称和喜欢+91,+255等数字现在我的按钮旁边我有在点击时接触选定它会显示在文本字段联系人号码打开一个联系人选择器,一个文本框。例如,我想要按钮标题标签为IN +91的情况,那么我希望将该+91附加到显示在文本字段中的联系电话上。我该如何实现
我的用于将联系人号码设置为文本字段并将国家/地区代码设置为按钮标题标签的代码:
//用于联系人选择代码
extension NeighbourDetailsVC: CNContactPickerDelegate
{
func contactPicker(_ picker: CNContactPickerViewController, didSelect contact: CNContact)
{
let phoneNumbersCount = contact.phoneNumbers.count
guard phoneNumbersCount > 0 else
{
dismiss(animated: true)
let alertController = UIAlertController(title: "Select contact doesn't have a number", message: nil, preferredStyle: .alert)
let cancelAction = UIAlertAction(title: "Cancel", style: .destructive, handler: {
alert -> Void in
})
alertController.addAction(cancelAction)
return
}
if phoneNumbersCount == 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...phoneNumbersCount-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: "").replacingOccurrences(of: ")", with: "")
contactNumber = contactNumber.replacingOccurrences(of: " ", with: "")
guard contactNumber.count >= 10 else
{
dismiss(animated: true)
print("selected contact doesn't have a valid number")
return
}
self.mobileTxtField.text = String(contactNumber.suffix(10))
}
}
//用于设置按钮标题标签
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
select_code = c_code[indexPath.row]
select_c_s_name = key[indexPath.row]
self.countryCodeBtn.setTitle(select_c_s_name+" +"+select_code, for: .normal)
self.get_country_data()
}