UIViewControllerRepresentable UIAlertController not able to enable/disable action? - ios

I want to use an alert with textField in my swiftUI app so I made following wrapper for UIAlertController. I want to disable my save button when there is no text in textField and enable it the moment user enters some text, but for some reason it is not happening, the button is stuck to the initial state.
code
struct UIAlertControllerWrapper: UIViewControllerRepresentable {
#Binding var text: String
#Binding var showAlert: Bool
var title: String
var message: String
var placeholder: String
var action: () -> Void
func makeUIViewController(context: Context) -> some UIViewController {
return UIViewController()
}
func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {
if showAlert {
let alert = createAlert(context)
context.coordinator.alert = alert
alert.actions[1].isEnabled = false
DispatchQueue.main.async {
uiViewController.present(alert, animated: true) {
showAlert = false
}
}
}
}
class Coordinator: NSObject, UITextFieldDelegate {
var parent: UIAlertControllerWrapper
var alert: UIAlertController?
init(_ parent: UIAlertControllerWrapper) {
self.parent = parent
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if let text = textField.text as? NSString {
parent.text = text.replacingCharacters(in: range, with: string)
} else {
parent.text = ""
}
guard let alert = alert else {
return true
}
if parent.text.trimmingCharacters(in: .whitespaces).isEmpty {
alert.actions[1].isEnabled = false
} else {
alert.actions[1].isEnabled = true
}
return true
}
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
func createAlert(_ context: Context) -> UIAlertController {
let alert = UIAlertController(
title: title,
message: message,
preferredStyle: .alert
)
alert.addTextField { textField in
textField.placeholder = placeholder
textField.text = text
textField.delegate = context.coordinator
}
let cancel = UIAlertAction(title: "Cancel", style: .cancel) { _ in
alert.dismiss(animated: true) {
showAlert = false
}
text = ""
}
let save = UIAlertAction(title: "Save", style: .default) { _ in
action()
alert.dismiss(animated: true) {
showAlert = false
}
text = ""
}
alert.addAction(cancel)
alert.addAction(save)
return alert
}
}

Related

SwiftUI: UIAlertController's textField does not responding in UIAlertAction

I need to use UIAlertContoller, as SwiftUI's Alert does not support TextField.
I must not use Custom created AlertView, due to various reason(Accessibility, DynamicType, Dark Mode support etc).
Basic Idea is, SwiftUI's alert must hold TextField & entered text must be reflect back for usage.
I created a SwiftUI view by Conforming to UIViewControllerRepresentable following is working code.
struct AlertControl: UIViewControllerRepresentable {
typealias UIViewControllerType = UIAlertController
#Binding var textString: String
#Binding var show: Bool
var title: String
var message: String
func makeUIViewController(context: UIViewControllerRepresentableContext<AlertControl>) -> UIAlertController {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
alert.addTextField { textField in
textField.placeholder = "Enter some text"
}
let cancelAction = UIAlertAction(title: "cancel", style: .destructive) { (action) in
self.show = false
}
let submitAction = UIAlertAction(title: "Submit", style: .default) { (action) in
self.show = false
}
alert.addAction(cancelAction)
alert.addAction(submitAction)
return alert
}
func updateUIViewController(_ uiViewController: UIAlertController, context: UIViewControllerRepresentableContext<AlertControl>) {
}
func makeCoordinator() -> AlertControl.Coordinator {
Coordinator(self)
}
class Coordinator: NSObject, UITextFieldDelegate {
var control: AlertControl
init(_ control: AlertControl) {
self.control = control
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if let text = textField.text {
self.control.textString = text
}
return true
}
}
}
// SwiftUI View in some content view
AlertControl(textString: self.$text,
show: self.$showAlert,
title: "Title goes here",
message: "Message goes here")
Problem:
There is No activity in Alert Action when it is tapped. I put breakpoints to check, but it never hit there.
Even UITextFieldDelegate's function never hit.
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool
Edit: cancelAction or submitAction does not triggers on tap of these fields.
Here is full demo module for solution that works. Tested with Xcode 11.4 / iOS 13.4
See also important comments inline
struct AlertControl: UIViewControllerRepresentable {
#Binding var textString: String
#Binding var show: Bool
var title: String
var message: String
func makeUIViewController(context: UIViewControllerRepresentableContext<AlertControl>) -> UIViewController {
return UIViewController() // holder controller - required to present alert
}
func updateUIViewController(_ viewController: UIViewController, context: UIViewControllerRepresentableContext<AlertControl>) {
guard context.coordinator.alert == nil else { return }
if self.show {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
context.coordinator.alert = alert
alert.addTextField { textField in
textField.placeholder = "Enter some text"
textField.text = self.textString // << initial value if any
textField.delegate = context.coordinator // << use coordinator as delegate
}
alert.addAction(UIAlertAction(title: "cancel", style: .destructive) { _ in
// your action here
})
alert.addAction(UIAlertAction(title: "Submit", style: .default) { _ in
// your action here
})
DispatchQueue.main.async { // must be async !!
viewController.present(alert, animated: true, completion: {
self.show = false // hide holder after alert dismiss
context.coordinator.alert = nil
})
}
}
}
func makeCoordinator() -> AlertControl.Coordinator {
Coordinator(self)
}
class Coordinator: NSObject, UITextFieldDelegate {
var alert: UIAlertController?
var control: AlertControl
init(_ control: AlertControl) {
self.control = control
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if let text = textField.text as NSString? {
self.control.textString = text.replacingCharacters(in: range, with: string)
} else {
self.control.textString = ""
}
return true
}
}
}
// Demo view for Alert Controll
struct DemoAlertControl: View {
#State private var text = ""
#State private var showAlert = false
var body: some View {
VStack {
Button("Alert") { self.showAlert = true }
.background(AlertControl(textString: self.$text, show: self.$showAlert,
title: "Title goes here", message: "Message goes here"))
Text(self.text)
}
}
}

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. 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?
I'm running it on 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
You should use textFieldShouldBeginEditing method. Open the contacts controller in this method and return false, no need to add a gesture recogniser.

Selected text doesn't appear on the UITextField

Whenever I click on a UITextField, a list of contacts appear and when a contact is clicked, the phone number should appear in the textfield that was clicked. I currently have 3 textfields and each time I select a contact, it updates only the first textfield even if for example I have selected the 2nd textfield. How do I go about fixing it so that the phone number appears in the corresponding textfield that was selected?
I'm using Xcode 10 and I think that the issue is arising from the 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
}
The reason your solution is updating only one text field is because you're updating the text of only that text field. In this line phonenumber.text = String(contactNumber.suffix(10)) you change only phonenumber's text. A good solution would be as follows:
Create a temp UITextField to store selected text field reference
#IBOutlet weak var phonenumber: UITextField!
#IBOutlet weak var phonenumber1: UITextField!
#IBOutlet weak var phonenumber2: UITextField!
var currentTextField: UITextField?
And use that text field in text field delegate methods
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
}
}
Assign selected contact number in that text field
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))
}

Check UITextfield interval of numbers

How can I check if the value from an UITextField is an interval of numbers between 1 - 100 and IF the number IS in that interval to send the value to another UIViewController? If the value in not in that interval then to show an alert.
My other controller have a var receivedValue = "" which I will use it to populate a UILabel.
Here is my code:
import UIKit
class ChildViewController: UIViewController {
#IBOutlet weak var insertNumberTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
insertNumberTextField.delegate = self
}
#IBAction func checkNumberIntervalButton(_ sender: UIButton) {
if insertNumberTextField.text == "\(1..<100)"{
print("Number is in interval 1 - 100.")
navigationController?.popViewController(animated: true)
dismiss(animated: true, completion: nil)
} else {
let alert = UIAlertController(title: "Try again", message: "Sorry but this numer is not in the inverval 1 - 100. Try again.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
present(alert, animated: true, completion: nil)
insertNumberTextField.text = ""
print("Number is not in the inverval 1 - 100.")
}
}
}
extension ChildViewController: UITextFieldDelegate {
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let allowedCharacters = CharacterSet.decimalDigits
let characterSet = CharacterSet(charactersIn: string)
return allowedCharacters.isSuperset(of: characterSet)
}
}
Convert the text field's text into an Int and compare it that way.
if let text = insertNumberTextField.text, let value = Int(text), (0 ..< 100).contains(value) {
// number is between 1 and 100
} else {
// show alert
}

How to hide keyboard when user taping return key

i'm a swift programmer, i have UIAlertViewController With two text fields and i want that when user taping enter , keyboard hide . but i don't know what i should do. its my code
var alert = UIAlertController(title: "خوش آمدید", message: "لطفا نام و نام خانوادگی خود را نام ببرید" , preferredStyle: UIAlertControllerStyle.Alert)
var act = UIAlertAction(title: "تایید", style: UIAlertActionStyle.Destructive, handler: {(ACTION) in
var name = alert.textFields![0] as! UITextField
var lname = alert.textFields![1] as! UITextField
self.namet = name.text
self.lnamet = lname.text
if (self.lnamet == "" && self.namet == "")
{
self.presentViewController(alert, animated: true, completion: nil)
}
self.defaults.setObject("done", forKey: "dn")
var fulltext = " خوش اومدی " + self.namet + self.lnamet
self.txt.text = fulltext
var ntfc = UILocalNotification()
ntfc.alertBody = " برگرد " + self.namet + "!"
ntfc.fireDate = NSDate(timeIntervalSinceNow: 40)
ntfc.soundName = "Untitled.m4a"
UIApplication.sharedApplication().scheduleLocalNotification(ntfc)
})
alert.addTextFieldWithConfigurationHandler { (textField) -> Void in
textField.placeholder = "نام"
textField.clearButtonMode = UITextFieldViewMode.Always
}
alert.addTextFieldWithConfigurationHandler { (textField1) -> Void in
textField1.placeholder = "نام خانوادگی"
textField1.clearButtonMode = UITextFieldViewMode.Always
}
First in your ViewController implement this UITextFieldDelegate
For eg.
class MyViewController: UIViewController, UITextFieldDelegate {
....
}
Now add a delegate to a TextField in which you want to dismiss the keyboard when return is tapped either in viewDidLoad method like below or where you are initialising it.
For eg.
override func viewDidLoad() {
super.viewDidLoad()
myTextField.delegate = self
}
Now add this method.
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}

Resources