I'm doing a form in an iPhone application. I'm setting the next field on the return of the keyboard. Everything works fine but if the text field is hidden under the keyboard, it's not scrolling on top of the keyboard dynamically! How can I do this?
Here is my code
It basically set to the next tag (next fileld)
-(BOOL)textFieldShouldReturn:(UITextField*)textField;
{
NSInteger nextTag = textField.tag + 1;
// Try to find next responder
UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
if (nextResponder) {
// Found next responder, so set it.
[nextResponder becomeFirstResponder];
} else {
// Not found, so remove keyboard.
[textField resignFirstResponder];
}
return NO; // We do not want UITextField to insert line-breaks.
}
Check out this question for sample code:
How programmatically move a UIScrollView to focus in a control above keyboard?
It sounds like what you want to use is the scrollRectToVisible:animated: method.
Here's documentation for UIScrollView to read more about the above method:
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIScrollView_Class/Reference/UIScrollView.html
Related
I have a UIPickerView set up with an attached toolbar button that should allow the user to switch from a picker view to a keyboard view. The pickerView should slide down and a keyboard should slide up in it's place... Theoretically.
The pickerView appears when a user clicks the text field in textFieldDidBeginEditing.
elementPicker = [[UIPickerView alloc] init];
[elementPicker setDelegate:self];
elementPicker.showsSelectionIndicator = YES;
...
[_itemElementField setInputView:elementPicker];
When the user clicks the "Use Keyboard" button, I have a method to dismiss the picker and call the keyboard. I can dismiss the pickerview without a problem but CANNOT DISPLAY THE KEYBOARD!!
HELP!
Here is the method called when the user wants the keyboard:
-(void)useKeyboardClicked:(id)sender {
NSLog(#"'USE KEYBOARD' BUTTON CLICKED");
// DISMISS THE PICKER VIEW
[elementPicker removeFromSuperview];
[_itemElementField resignFirstResponder];
_itemElementField.inputAccessoryView = nil;
// SET ELEMENTPICKER TO THE DEFAULT KEYBOARD
elementPicker = UIKeyboardTypeDefault;
[_itemElementField setInputView:elementPicker];
// SHOW KEYBOARD
[_itemElementField becomeFirstResponder];
}
I'm grasping at straws now and need some help! I've even gone so far as to try to define a keyboard in the header file with
#property (strong, nonatomic) IBOutlet UIKeyboard *elementKeyboard;
but that doesn't work AT ALL.
for those that might have this issue in the future, the answer ends up being quite simple.
You need to reload the input view after resetting it to a keyboard with reloadInputViews.
Here's the code:
-(void)useKeyboardClicked:(UITextField *)sender {
NSLog(#"'USE KEYBOARD' BUTTON CLICKED");
[_itemElementField setInputView:UIKeyboardTypeDefault];
[_itemElementField becomeFirstResponder];
[_itemElementField reloadInputViews];
}
Here Is Solution Firstly you have to declare boolean variable checkFlag to keep record of useKeyboardClicked:
then
-(void)useKeyboardClick:(id)sender
{
if (checkFlag) {
checkFlag=false;
[txtField setInputView:nil];
[txtField reloadInputViews];
[txtField setKeyboardAppearance:UIKeyboardAppearanceDefault]; //you can set UIKeyboardAppearnce as Dark,light, alert instead of Default
[txtField setKeyboardType:UIKeyboardTypeDefault]; // you set decimal keyboard
} else {
checkFlag=TRUE;
[txtField setInputView:nil];
[txtField setInputAccessoryView:numberToolbar];
[txtField setInputView:pickerView];
}
}
One thing i m not total wrong but i can't set back picker view instead keyboard ..... try this i gave you hint ...
I'm making a login screen for my app and i want the return button to say "Next" when there is a field that is not yet filled out and it sould say "Go" when all fields are filled (i'm talking about UITextFields).
The code below works fine in that it shows Next and Go at the correct moments. But whenever it changes from "Next" to "Go" the next tap on the keyboard is ignored. When it says "Go" and i empty the textfield by backspacing there is no such problem and it shows Next as it should. It's almost like the old keyboard is still there and vanishes after being tapped.
My question is: what is the source of this problem and more importantly how do i get rid of this freezing up of the keyboard?
UITextField *theSender = (UITextField *)sender;
if (allTextFieldsAreFilled) {
if (theSender.returnKeyType!=UIReturnKeyGo) {
theSender.returnKeyType = UIReturnKeyGo;
[theSender resignFirstResponder];
[theSender becomeFirstResponder];
}
} else {
if (theSender.returnKeyType!=UIReturnKeyNext) {
theSender.returnKeyType = UIReturnKeyNext;
[theSender resignFirstResponder];
[theSender becomeFirstResponder];
}
}
This code gets called every time the value of one of the three UITextFields is changed, so it is an IBAction connected to the Editing Changed event.
Thanks in advance for your help!
EDIT
I found out this will only occur if the textfield is set to secure (password). When it is not set to secure it will not freeze up and my code works perfectly! The problem is that the change to the "Go" button will generally occur when a 'secure' textfield is fist responder. So this doesn't change anything to the problem.
I created a new Single View Application project to test this out; and dropped the following code in the ViewController.m
#import "ViewController.h"
#interface ViewController () <UITextFieldDelegate>
{
#private
IBOutlet UITextField* m_fieldA;
IBOutlet UITextField* m_fieldB;
IBOutlet UITextField* m_fieldC;
}
#end
#implementation ViewController
// connected to Editing Did Begin
- (IBAction) onFocus:(UITextField*)_textField
{
[self updateKeyboardFor:_textField];
}
// connected to Editing Changed
- (IBAction) onChanged:(UITextField*)_textField
{
[self updateKeyboardFor:_textField];
}
- (void) updateKeyboardFor:(UITextField*)_textField
{
bool allTextFieldsAreFilled = [m_fieldA.text length] && [m_fieldB.text length] && [m_fieldC.text length];
if (allTextFieldsAreFilled)
{
if (_textField.returnKeyType != UIReturnKeyGo)
{
_textField.returnKeyType = UIReturnKeyGo;
//[_textField resignFirstResponder];
//[_textField becomeFirstResponder];
[_textField reloadInputViews];
}
}
else
{
if (_textField.returnKeyType != UIReturnKeyNext)
{
_textField.returnKeyType = UIReturnKeyNext;
//[_textField resignFirstResponder];
//[_textField becomeFirstResponder];
[_textField reloadInputViews];
}
}
}
// A part of UITextFieldDelegate
- (BOOL) textFieldShouldReturn:(UITextField*)_textField
{
if (_textField.returnKeyType == UIReturnKeyGo)
{
[_textField resignFirstResponder];
// go off and perform 'go'
}
else
{
if(_textField == m_fieldA) [m_fieldB becomeFirstResponder];
if(_textField == m_fieldB) [m_fieldC becomeFirstResponder];
if(_textField == m_fieldC) [m_fieldA becomeFirstResponder];
}
return true;
}
#end
Then in the XIB create three UITextFields and hooked them up to the IBOutlets, IBActions, and also set this view controller as the delegate for all the fields.
All seems to work fine regardless of secure fields.
My guess is that your issue is somewhere in the code where you move to the 'Next' field; and not in the code that you posted. Also make sure that all your outlets and delegates are linked up properly.
Update: I've edited the code above. The commented out lines were the problem, you should be using reloadInputViews to update the button.
It seems that it wasn't locking the keyboard; but what it was doing was after you typed the first letter and did the resign/become calls to refresh the button then the second press would overwrite the first. This seems like a bug in iOS... it's more noticeable if after the first letter you type a space. When I noticed that I added a label that updated it's content with the contents of the password field and it was even more clear what was happening.
I'm trying to get the keyboard to disappear when the screen is touched, a question that is answered all over stackoverflow. I was able to get the keyboard to disappear when the enter key was pressed thanks to a thread here. I'm not having luck on the background touch resigning the first responder. The method is being entered, I have an NSLog in the method saying, "in backgroundTouched" but the keyboard is still there.
I've tried making the UIView a UIControl class so I could use the touch event.
journalComment is a UITextView.
-(IBAction)backgroundTouched:(id)sender
{
[journalComment resignFirstResponder];
NSLog(# "in backgroundTouched");
}
I've also tried having a invisible button under everything that calles the backGroundTouched method. I think it maybe that I'm missing something in interface builder, but I'm not sure what.
Thank you for any help!
This is what works for the done button:
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range
replacementText:(NSString *)text
{
// Any new character added is passed in as the "text" parameter
if ([text isEqualToString:#"\n"]) {
// Be sure to test for equality using the "isEqualToString" message
[textView resignFirstResponder];
// Return FALSE so that the final '\n' character doesn't get added
return FALSE;
}
// For any other character return TRUE so that the text gets added to the view
return TRUE;
}
I found the following code works best with my text view (not text field) without the delegate methods:
first you set up a tap gesture recognizer onto your view :
- (void)viewDidLoad{
UITapGestureRecognizer* tapRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(tap:)];
tapRecognizer.delegate = self;
[self.view addGestureRecognizer:tapRecognizer];
}
and then in your tap method :
- (void)tap:(id)sender
{
// use to make the view or any subview that is the first responder resign (optionally force)
[[self view] endEditing:YES];
}
this should allow your keyboard to be dismissed when you anywhere on the view.
Hope this helps
Try this. We had this problem eariler, but eventually found the right solution.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[yourtextfield resignFirstResponder];
// you can have multiple textfields here
}
This should resolve the problem with the keyboard not dissapearing when pushing the background.
is there anyboby who can give me an example method that is called by pressing the return button of the keyboard and saves the text of a textview (that was typed in before) in the nsuserdefaults?
thanks a lot :)
Make sure your UITextField has a return key type set to UIReturnKeyGo (this is for the image on the keyboard):
theTextField.returnKeyType = UIReturnKeyGo;
Then use this method to do what ever you want to do:
- (BOOL) textFieldShouldReturn:(UITextField *)textField
{
// Tell the keyboard where to go on next / go button.
if(textField == theTextField)
{
// do stuff
}
return YES;
}
To get the text from the textfield just call theTextField.text and save as you wish!
Swift Version
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
// Tell the keyboard where to go on next / go button.
if textField == theTextField {
// do stuff
}
return true
}
If you are adding UITextField to an UITableCell dynamically, you need to also set delegate for it:
self.textfield.delegate = self;
also on the the header file you need to add this:
#interface YourController: UIViewController <UITextFieldDelegate>
I have 3 uitextfield in my project
I need to when tap inside one of them (uitextfield2 ) a custom subview appear , and need the key board to appear when tap on one another (uitextfield1 )
the problem is when I tab on uitextfield1 , the keypad appear and not go even I clicked return or tap on another uitextfield2
I need the keyboard to disappear when I click out of the uitextfield1 or when click return
I use the following code
- (BOOL)textFieldShouldReturn:(UITextField *)textField { // When the return button is pressed on a textField.
[textField resignFirstResponder]; // Remove the keyboard from the view.
return YES; // Set the BOOL to YES.
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
// [textField resignFirstResponder];
[self SelectModalityClick]; // will be called If i tapped inside the uitextfield2 to display the custom view
return NO;
}
When you create your UITextField instances, you can set the inputView to whatever UIView subclass you want with something like this.
UITextField *aTextField = [[UITextField alloc] initWithFrame:aRect];
aTextField.inputView = myCustomInputView; // Set this up in your viewDidLoad method
aTextField.delegate = self;
aTextField.tag = MyCustomInputView; // #define this as some integer
[self.view addSubview];
[aTextField release];
You shouldn't need to do anything special to make the correct input view appear, if you have a UITextField instance variable for the first responder, just assign it in textFieldShouldBeginEditing: and resign its first responder status in textFieldShouldReturn:.