1

I have a custom textfield. It opens a pop-up using UITableViewController, when user taps on it.

I wanted to prevent keyboard to pop-up for my text field, looks like it is not possible.

So I tried the following code which works in simulator, but it does not work for an actual iPhone!!!

    @IBAction func provincePressed(_ sender: Any) {
         (sender as AnyObject).resignFirstResponder()
         self.view.endEditing(true)

What is wrong? and how can I make it work for an actual iPhone? or possibly prevent keyboard to show up at all!

Iman Nia
  • 2,255
  • 2
  • 15
  • 35
  • So what's the purpose of tapping a textfield without displaying the keyboard? What are you aiming to achieve? if there is an irrelevant functionality, you could use a button instead... – Ahmad F Feb 24 '19 at 12:54
  • He is opening some kind of menu to select, i think so? – Harjot Singh Feb 24 '19 at 13:09
  • @AhmadF Its neither a text field nor a button - in nature. I have subclassed `UITextField` to create a custom widget for what I am about to do. I want the keyboard to be active in case the internet connection is not available,However I do not need a keyboard while there is an internet connection. there is some logic behind using textfield, believe me. – Iman Nia Feb 24 '19 at 13:41
  • @HarjotSingh Yes, Thats what I am doing and I want to write the selected item in my text field. – Iman Nia Feb 24 '19 at 13:42
  • Ok then put a button over the text field and show menu no need to add extra code for the textfield.Putting button over textfield makes all the four answers below useless. – Harjot Singh Feb 24 '19 at 13:43
  • @HarjotSingh I need to provide a keyboard in special circumstances. It needs to be a TextField. – Iman Nia Feb 24 '19 at 13:44
  • @Zich then i am adding an answer read check and accept it. – Harjot Singh Feb 24 '19 at 13:45

5 Answers5

1

Try to return false in the textFieldShouldBeginEditing method

func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
  return false
}
Stefo
  • 636
  • 1
  • 8
  • 13
1

Set the text field’s isEnabled to false beforehand.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • "I have a custom textfield. It opens a pop-up using UITableViewController, when user taps on it." Don't you think that setting `isEnabled` to `false` would prevent what mentioned (the "tapping")? If I'm not mistaking, I think that it should be even a button instead of textfield for this case... – Ahmad F Feb 24 '19 at 13:24
0

Swift 4

You can just call textField.resignFirstResponder() inside textFieldDidBeginEditing method like this :

-(void)textFieldDidBeginEditing:(UITextField *)textField {     
    if (textField.tag == 1) {
        //this is textfield 2, so call your method here
       textField.resignFirstResponder()
    }
}

You should give the wanted textfield a tag. If you want the same behavior for all textfields in your ViewContriler then that part is not needed.

Also, don't forget to extend your ViewController from UITextFieldDelegate and delegate the textField to your ViewController .

textField.delegate = self

koen
  • 5,383
  • 7
  • 50
  • 89
0

This is according to the SafoineMoncefAmine Answer.

-(void)textFieldDidBeginEditing:(UITextField *)textField {     
    if (textField == yourTextfield) {// No need tag Use QA Manager to handle the textfield

       textField.resignFirstResponder()
       // Open your menu here, add view, show the menu here.

    }
}

In addition to this answer,

extension MyViewController: UITextFieldDelegate {
    func textField(_ textField: UITextField,
                   shouldChangeCharactersIn range: NSRange,
                   replacementString string: String) -> Bool {
        if textField == YourTextField{
        return false
        }
        else{
        return true
        }
    }
}
Harjot Singh
  • 535
  • 7
  • 24
0

You can just disable user interaction from UITextField , and cover it with UIView and add uigesturerecognizer to that view to trigger your event .