Show UITextField keyboard on firstResponder even when userInteractionEnabled = NO - iphone

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!

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

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.

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

Related

How to hide the iPhone keyboard? resignFirstResponder does not work

I have the following code...
- (void)textFieldDidBeginEditing:(UITextField *)textField {
//some code here...
NSInteger theTag = textField.tag; //I set the tag to 5 in IB
if (theTag == 5) {
//self.showDatePicker;
[textField resignFirstResponder];
}
}
The problem is, the keyboard never disappears. Another thing to note is that I have some other methods that move the view up and down based on the position of the textfield selected. Maybe that's messing up my Responder, but I just don't understand why the keyboard won't go away.
Also, I might just be doing this all wrong. I want this textField, when pressed, to hide the keyboard and show a date picker. Should this be in a different method?
If you're targeting iOS 3.2+, I'd suggest looking into UITextField's inputView property. You can assign a custom view (i.e. a data picker) to be displayed when the text field becomes the first responder instead of the keyboard.

Setting a UITextField into editing mode programmatically

I have a UITextField that I want to set into editing mode (keyboard on screen and cursor in text field box) programatically. I know that the user will be in editing mode when this view appears onscreen, so I want to save the user from having to tap the text field.
The "editing" property of a UITextField is read only - so that doesn't work. Is there a way to set the UITextField into editing mode, with a keyboard onscreen, programmatically?
Call becomeFirstResponder on the UITextField.
Related question:
How do I show the keyboard by default in UITextView?
You have to call [textField becomeFirstResponder];
Indeed call [textField becomeFirstResponder] in Obj-C or textField.becomeFirstResponder() in Swift.
However, make sure you call this in the viewDidAppear and not in the viewDidLoad to prevent strange behaviour (see: When set UITextField as FirstResponder programmatically, cause some weird actions on text editing).

UIButton not disabled when UITextField has focus

I have a UIButton - a submit button - that I set the enabled and disabled states and titles for. I use the submit button title to show status like #"Sending..." while my program is making an api call by disabling the button.
It works fine until the edge case where someone enters a comment on a UITextField, but instead of dismissing the keyboard/finishing editing just hits the SUbmit button (so the keyboard is still up).
In this case, disabling the button doesn't change the title and background to the disabled state.
Is this an issue with firstResponder? I'm trying to explicity tell the comment field to resignFirstResponder before setting the button.enabled = NO, but it still doesn't update the button.
Are you implementing this UITextFieldDelegate method:
- (void)textFieldShouldEndEditing:(UITextField *)textField {
[textField resignFirstResponder];
button.enabled = NO;
}
and then resigning the text field's first responder and disabling the button?
Sometimes textFieldSHouldEndEditing doesn't get called since the field may not lose focus, so I do try to explicity tell the text field to resign before disabling the button. Doesn't seem to work...

How do I prevent my table cell's textview from being editable after editing is done?

So this is an interesting problem. I have custom tableviewcells that include a text field. When In my cellForRowAtIndexPath I have an if statement that determines whether or not the cell's text field should be editable- it looks like this:
(self.isEditing) ? [infoCell.textField setEnabled:YES] : [infoCell.textField setEnabled:NO];
This actually works well - except for the issue I'm having. It makes it so that when the tableview is displayed, the rows' text field cannot be edited. When the user clicks "Edit" to put it into editing mode, then the text fields are enabled for editing.
The Problem: When I am editing a field, and click "Done", it goes back to the regular tableview but the keyboard stays visible and the last cell's text field I was editing continues to be editable.
What Should happen: The keyboard should go away and all the cells' text fields should no longer be editable.
Any ideas about what could be going wrong? Things to look for?
Thanks!
Unfortunately, disabling the UITextField won't dismiss the keyboard. You'll need to retain a pointer to your current UITextField. First, create an instance variable in your header file:
UITextField *currentTextField;
Then, implement the UITextFieldDelegate protocol. The dirty work will be done in the following method:
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
currentTextField = textField;
}
Finally, when you're ready to dismiss the keyboard and disable your textFields, simply call:
[currentTextField resignFirstResponder];
[textField1 setEnabled:NO];
[textField2 setEnabled:NO]; //ad nauseum
Good luck!
I've found self.isEditing to be unreliable. If you are editing an individual cell, it works differently from when you are in "edit mode".
What I've done to get around it is, whenever I want to do something to all other cells, I just iterate through my table view's visibleCells method and manually adjust them. You'll have to consider what happens when new cells become visible, but that's up to your implementation.
NSArray *visibleCells = [self.tableView visibleCells];
for (UITableViewCell *cell in visibleCells) {
[cell doSomething];
}
PS - obviously you may want to skip the cell in question when iterating through the visible squares. depends on what you're doing.

iPhone: is there another way of yielding First Responder status?

Problem: If you have a big form with a lot of text input fields with number pad, you would not want to tell every single text field something like
[myTextField1 resignFirstResponder];
[myTextField2 resignFirstResponder];
instead, it would be great to just tell for example an invisible background button, that it is the First Responder as soon as the user tabs outside of any text field.
Like in JavaScript, when you give an element the focus(), all others lose it. How can I do that in UIKit?
[button becomeFirstResponder];
In your header file, paste this: -(IBAction)backgroundClicked:(id)sender;
in your implementation file, paste this:
(IBAction) backgroundClicked:(id)sender
{
[nameField resignFirstResponder];
[numberField resignFirstResponder];
}
In InterfaceBuilder, create a button that covers the entire view.
with the button selected, click Layout > Send to Back
Control drag from the button to File's Owner and select the outlet called: backgroundClicked.