How to disable UITextField editing, but still accept the touch?

I am doing a UITextField with a UIPickerView as inputView . All this is good, except that I can edit by copying, pasting, cutting and selecting text, and I do not want this. Only the collector should change the text box.

I found out that I can disable editing by setting setEnabled or setUserInteractionEnabled:NO to NO . Good, but the TextField stops responding to the touch, and the collector does not appear.

What can I do to achieve it?

+72
ios uitextfield uipickerview
Feb 02 2018-12-12T00:
source share
14 answers

Using a text field delegate, there is a method

 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 

Refuse NO, and any attempts by the user to edit the text will be rejected.

Thus, you can leave the field turned on, but still prohibit inserting text into it.

+100
Feb 02 '12 at 17:50
source share

This will be the easiest of all:

in viewDidLoad: (set the delegate only for text fields that should not be edited.

 self.textfield.delegate=self; 

and paste this delegate function:

 - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{ return NO; } 

Here it is!

+17
Jul 08 '13 at 12:51 on
source share

It would be more elegant to create a custom subclass of UITextField that returns NO for all canPerformAction:withSender: calls (or at least where the action is @selector(cut) or @selector(paste) ), as described here .

In addition, I would also implement - (BOOL) textField: (UITextField *) textField shouldChangeCharactersInRange: (NSRange) range replacementString: (NSString *) string according to Nick's suggestion to disable text input from the Bluetooth keyboard.

+5
Feb 02 2018-12-12T00:
source share

Translate Nick's answer to fast:

P / S: return false => text fields cannot be entered, edited from the keyboard. It just can set the text by code. EX: textField.text = "My String Here"

 override func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { return false } 
+5
Jul 24 '15 at 8:14
source share

In Swift:

 func textFieldShouldBeginEditing(textField: UITextField) -> Bool { questionField.resignFirstResponder(); // Additional code here return false } 
+5
Jun 20 '16 at 17:06
source share

Just place the UIButton exactly throughout the UITextField with no label text that makes it "invisible." This button can accept and delegate strokes instead of a text field, and the contents of the TextField are still visible.

+3
Jan 25 '15 at 22:58
source share

For an alternative that handles UIPickerView and action tables, checkout ActionSheetPicker

https://github.com/TimCinel/ActionSheetPicker

Cocoapods are included. It handles all cancel and end buttons in the Action Sheet. The examples from the project project are great. I choose an ActionSheetStringPicker that only handles String-based parameters easily, but the API can handle most of everything I can think of.

I initially started a solution similar to a security question, but came across this project and took about 20 minutes to integrate it into the application for use, including using cocopods: ActionSheetPicker (~> 0.0)

Hope this helps.

Download the git project and look at the following classes:

  • ActionSheetPickerViewController.m
  • ActionSheetPickerCustomPickerDelegate.h

Here is just about most of the code I added, plus the import * .h.

 - (IBAction)gymTouched:(id)sender { NSLog(@"gym touched"); [ActionSheetStringPicker showPickerWithTitle:@"Select a Gym" rows:self.locations initialSelection:self.selectedIndex target:self successAction:@selector(gymWasSelected:element:) cancelAction:@selector(actionPickerCancelled:) origin:sender]; } - (void)actionPickerCancelled:(id)sender { NSLog(@"Delegate has been informed that ActionSheetPicker was cancelled"); } - (void)gymWasSelected:(NSNumber *)selectedIndex element:(id)element { self.selectedIndex = [selectedIndex intValue]; //may have originated from textField or barButtonItem, use an IBOutlet instead of element self.txtGym.text = [self.locations objectAtIndex:self.selectedIndex]; } -(BOOL)textFieldShouldBeginEditing:(UITextField *)textField { return NO; // Hide both keyboard and blinking cursor. } 
+1
Apr 2 '14 at 4:51 on
source share

I used the solution provided by MrMage. The only thing I would like to add is to cancel the UITextView as the first responder, otherwise you are stuck with the selected text.

Here is my quick code:

 class TouchableTextView : UITextView { override func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool { self.resignFirstResponder() return false } override func shouldChangeTextInRange(range: UITextRange, replacementText text: String) -> Bool { self.resignFirstResponder() return false } } 
+1
May 03 '15 at 2:51
source share

In fast 3+:

 class MyViewController: UIViewController, UITextFieldDelegate { override func viewDidLoad() { self.myTextField.delegate = self } func textFieldShouldBeginEditing(textField: UITextField) -> Bool { if textField == myTextField { // code which you want to execute when the user touch myTextField } return false } } 
+1
Oct 10 '17 at 10:59 on
source share

This workaround works. Place a transparent UIView over the text box and execute the following code:

 - (void)viewDidLoad { [super viewDidLoad]; UILongPressGestureRecognizer *press = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress)]; [transparentView addGestureRecognizer:press]; [press release]; press = nil; } -(void)longPress { txtField.userInteractionEnabled = NO; } -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { txtField.userInteractionEnabled = YES; } -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { [txtField becomeFirstResponder]; } 
0
Feb 02 '12 at 17:53
source share

Make your inputView represented by a hidden text box that will also change the text of the presented and disabled text.

0
Dec 25 '13 at 13:17
source share

To prevent editing a UITextField when using a UIPickerView to select values ​​(in Swift):

 self.txtTransDate = self.makeTextField(self.transDate, placeHolder: "Specify Date") self.txtTransDate?.addTarget(self, action: "txtTransDateEditing:", forControlEvents: UIControlEvents.EditingDidBegin) func makeTextField(text: String?, placeHolder: String) -> UITextField { var textField = UITextField(frame: CGRect(x: 140, y: 0, width: 220.00, height: 40.00)); textField.placeholder = placeHolder textField.text = text textField.borderStyle = UITextBorderStyle.Line textField.secureTextEntry = false; textField.delegate = self return textField } func txtTransDateEditing(sender: UITextField) { var datePickerView:UIDatePicker = UIDatePicker() datePickerView.datePickerMode = UIDatePickerMode.Date sender.inputView = datePickerView datePickerView.addTarget(self, action: Selector("datePickerValueChanged:"), forControlEvents: UIControlEvents.ValueChanged) } func datePickerValueChanged(sender: UIDatePicker) { var dateformatter = NSDateFormatter() dateformatter.dateStyle = NSDateFormatterStyle.MediumStyle self.txtTransDate!.text = dateformatter.stringFromDate(sender.date) } func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { self.resignFirstResponder() return false } 
0
Jun 18 '15 at 6:06
source share

I used:

 [self.textField setEnabled:NO]; 

and his work is excellent

-3
Aug 6 '13 at 11:09
source share

This worked for me [textview setEditable:NO]; The above answers are overly complex.

-6
Aug 05 '15 at 21:47
source share



All Articles