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

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.

Related

Show UITextField keyboard on firstResponder even when userInteractionEnabled = NO

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

delegate called on leaving the foucus on textbox

textFieldShouldEndEditing is calling only when leaving the focus from first text field and focus the second text field.. here i want to call the delegate when i click outside the text box rather than focus the other text box.
daclare an instance to UITextField: UITextField *currentTextField;
in textfieldDidBeginEditting say currentTextField = textField;
place a custom button without image and title all over the screen and give it the action below:
Now your delegate will get called when you tap wherever on the screen (excluding other controls).
-(IBAction)hideCurrentKeyboard{
[currentTextField resignFirstResponder];
}

How to call numpad using action button not from text field

I know this is silly question but as a beginner at xcode, and i can't find the answer from any source, can i call numpad (numberic keyboard) from my action button? I just want my action button can call numpad like text field do. what code should i insert in my action button?
my code:
- (IBAction)setLoopBtn:(id)sender
{
}
Thank you again for sharing your knowledge to noob like me.
Create a UITextField with a frame that puts it off the visible screen. Assign the type of keyboard you want to display as the default for that field. When the button is tapped, send becomeFirstResponder to your off-screen field.
-(IBAction) ButtonClick
{
txtField.keyboardType = UIKeyboardTypeNumberPad;
[txtField becomeFirstResponder];
}

Working With Stacked UIButtons

Hey guys, so I have this search bar in my view along with many buttons below it. So when the search bar is tapped, a keyboard pops up, however I want the user to be able to click anywhere below the search bar and above the keyboard to get out of searching mode. I have been scavenging SO for a bit and came across a solution that suggested that I create an invisible button which intercepts touch events which I can use to resign first responder status from the search bar. And I can merely hide/disable the button when I do not need it so that the buttons below it may be tapped right? Wrong. setHidden nor setEnabled: aren't doing the trick. Here is the relevant code:
//touch event on button outsideSearchBarButton which is invisible
- (IBAction)selectOutsideSearchBar:(id)sender {
NSLog(#"Selected outside search bar");
[searchBar resignFirstResponder];
[outsideSearchBarButton setEnabled:NO];
[outsideSearchBarButton setHidden:YES];
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)aSearchBar {
NSLog(#"searchbarTextDidBeginEditing");
[outsideSearchBarButton setHidden:NO];
[outsideSearchBarButton setEnabled:YES];
}
Setting the button to disabled or hidden via nib file does not help me at all. Either the button never exists (I can't click between search bar and keyboard and have keyboard go away) or always exists (I can't click any of the buttons below this invisible button). Any help appreciated. Thanks in advance!
//touch event on button outsideSearchBarButton which is invisible
- (IBAction)selectOutsideSearchBar:(id)sender {
NSLog(#"Selected outside search bar");
[searchBar resignFirstResponder];
outsideSearchBarButton.userInteractionEnabled = NO;
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)aSearchBar {
NSLog(#"searchbarTextDidBeginEditing");
outsideSearchBarButton.userInteractionEnabled = YES;
}
Do this.
go to the interface builder and select the button on which you are removing the keyboard during search, then select layout from the above menu and select send to back.Then in the inspector window set the button style to custom.
Hope this helps you.......

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.