3

I have a UITextField that is first responder. I want to show keyboard when entering the view but I want to do that the user will not be able to edit it and the cursor will be hidden all time as well.

When you click on a keyboard letter, it will be written in the UITextField, but the user will not be able to edit nothing there, even not to copy.

Thanks!

ytpm
  • 4,962
  • 6
  • 56
  • 113
  • I think there must be a better solution to your problem than this approach. Just spitballing here, but maybe have a UITextField that is `hidden` that you call `becomeFirstResponder` on, and then in its delegate method `textField:shouldChangeCharactersInRange:replacementString:` you could take the text and add it to a `UITextView` or `UITextField` with `userInteractionEnabled = NO`. – MaxGabriel Dec 01 '12 at 17:46
  • I didn't understand what you mean here.. – ytpm Dec 01 '12 at 18:30

3 Answers3

6

Ok, per my comment, my solution is to have a surrogate UITextField that has its hidden property set to YES. What I do is add that hidden text field to the view, and call becomeFirstResponder on it. The user has no idea this text field exists. In the delegate callback from the text field, I take the text the user typed in and add it to a UITextView (though you could add the text to whatever you wanted, like a UITextField like in your question). I turn off userInteractionEnabled for the visible text view. This creates the effect you desire.

I created a sample project that I uploaded to Github. (If you aren't familiar with it, just click the zip button to download it, unzip it, and open the .xcodeproj file). https://github.com/MaxGabriel/HiddenTextField

MaxGabriel
  • 7,617
  • 4
  • 35
  • 82
  • WOW! Thank you very much for the solution and of course for the code itself man, it is really amazing what you did here! thanks! – ytpm Dec 02 '12 at 07:54
0

Edited answer: Try this:

1. [txtField becomeFirstResponder]; 
2. txtField.enabled = NO; 
3. when some press on keyboard, then txtField.enabled = YES;

Check this out : http://www.youtube.com/watch?v=cKV5csbueHA

James Webster
  • 31,873
  • 11
  • 70
  • 114
Dilip Lilaramani
  • 1,126
  • 13
  • 28
  • Property 'editable' not found on object of type 'UITextField', Why is that? – ytpm Dec 01 '12 at 17:11
  • It's working, you can't edit the UITextField, but the keyboard disappears.. how can I do that the keyboard will stay visible and also that I can click and edit the UITextField through it. – ytpm Dec 01 '12 at 17:22
0

I had a UISearchBar property in my viewController. And I did it like this:

- (void)viewWillAppear:(BOOL)animated
{
    [self.searchBar becomeFirstResponder];
}

This should work the same for a UITextField.

As for disabling editing, use:

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

You should have set your viewController to be the delegate of UITextField.

Abdullah Umer
  • 4,234
  • 5
  • 36
  • 65
  • Thats ok, but it's not what I'm asking for. He's already set as first responder, but I want it to be first responder, the keyboard will apeears, stay there but the UITextField will be set to editable = NO – ytpm Dec 01 '12 at 17:29
  • Hah.. Thats a very weird behavior you are looking for. :\ – Abdullah Umer Dec 01 '12 at 17:44