keyboard how to hide it - objective-c

I have a UIView with some UITextField, but when I click at them, I cant hide the keyboard and the "send" button is behind the keyboard:
how can I hide it?
Thanks!

Add UITextField's delegate method in your code.Don't forget to set the delegate.Whenever return key is keyboard is pressed this below method will be called. Also set its delegates:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
EDIT : when once focused to textField and focus to other textfield, that textfield will not resign so do this as i assume from topmost u have textField1, textField2, textField3, textField4 if any more.... Add this delegate method.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if(textField == textField1) //motive resign other textFields
{
[textField2 resignFirstResponder];
[textField3 resignFirstResponder];
[textField4 resignFirstResponder];
}
else if(textField == textField2) //motive resign other textFields
{
[textField1 resignFirstResponder];
[textField3 resignFirstResponder];
[textField4 resignFirstResponder];
}
else if(textField == textField3) //motive resign other textFields
{
[textField1 resignFirstResponder];
[textField2 resignFirstResponder];
[textField4 resignFirstResponder];
}
else if(textField == textField4) //motive resign other textFields
{
[textField1 resignFirstResponder];
[textField2 resignFirstResponder];
[textField3 resignFirstResponder];
}
return YES;
}
EDIT U can use this also:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
[self.view endEditing:YES];
return YES;
}

I use this code, this event runs when you tap anywhere in view, and you do not depend on textFields events:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if ([textField1 isFirstResponder] && [touch view] != textField1) {
[textField1 resignFirstResponder];
}
if ([textField2 isFirstResponder] && [touch view] != textField2) {
[textField2 resignFirstResponder];
}
...
[super touchesBegan:touches withEvent:event];
}

Related

resignFirstResponder not getting called inside textFieldShouldClear

I am trying to implement search bar in one of my page.
I am not using regular search bar due to its design.
What I have is as below.
UIImageView above UIView (textfield background)
UITextField above UIImageView (textfield)
I am using delegates for UITextField.
In code I have searchTF.clearButtonMode = UITextFieldViewModeWhileEditing; to show the clear button.
Search is working fine but the problem is in delegate of clear button.
I have below code
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
if (textField == searchTF) {
NSLog(#"clicked clear button");
[textField resignFirstResponder]; // this is not working
// also below is not working
// [searchTF resignFirstResponder];
}
return YES;
}
When I click clear button, I get NSLog of text "clicked clear button", however the keyboard doesn't get dismissed.
Any idea why keyboard is not getting dismissed when I have
Edit 1
Even I tried as below using [self.view endEditing:YES];, but still its not working.
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
if (textField == searchTF) {
[self.view endEditing:YES];
[self hideAllKeyboards];
}
return YES;
}
Additional to my comment I made some testing and here are my results:
I've just implemented a UITextField with all delegate methods like this:
- (BOOL)textFieldShouldClear:(UITextField *)textField {
NSLog(#"Should Clear");
[textField resignFirstResponder];
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
NSLog(#"Begin editing");
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
NSLog(#"End editing");
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
NSLog(#"Should begin editing");
return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
NSLog(#"Should end editing");
return YES;
}
- (BOOL)textField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string {
NSLog(#"Change char");
return YES;
}
As soon as you hit the clear button the log outputs:
2014-07-26 11:08:44.558 Test[36330:60b] Should Clear
2014-07-26 11:08:44.558 Test[36330:60b] Should end editing
2014-07-26 11:08:44.559 Test[36330:60b] End editing
2014-07-26 11:08:44.560 Test[36330:60b] Should begin editing
2014-07-26 11:08:44.561 Test[36330:60b] Begin editing
As you can see the shouldBeginEditingand the didBeginEditing methods get called after clearing so the resignFirstResponder in textFieldshouldClear gets called just before a new becomeFirstResponder is called by shouldBeginEditing or didBeginEditing.
I believe your textfield IBOutlet is properly connected ,Although you can try
[[[UIApplication sharedApplication] keyWindow] endEditing:YES];
I am not sure what was the problem, but I solved calling the dismiss keyboard method after some interval using NSTimer.
Below is what I did.
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
if (textField == searchTF) {
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:#selector(hideAllKeyboards) userInfo:nil repeats:NO];
}
return YES;
}
-(void) hideAllKeyboards {
[searchTF resignFirstResponder];
}
This way, after clicking the clear button, keyboard is getting dismissed.
It would be great if someone post answer as why this is happening. I will mark that answer as accepted.
Edit 1
As per Daniel answer, I did below.
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
if (textField == searchTF) {
isFilterOn.text = #"no";
[textField resignFirstResponder];
textField.text = #"";
[self myTextFieldDidChange];
}
return NO;
}
In myTextFieldDidChange, I am showing the filtered list and un-filtered list based on the text I have in UITextField.

Can i update value of label by long press on it - iOS

I write a simple application with Xcode 5 on iPhone iOS7 device.
I have a label that increments by +/- Buttons, but i want to give option for user to insert his number to this label.
How can i do it with long press recogniser?
Thanks.
Use a UILongPressGestureRecognizer and a UITextView.
Add a UILongPressGstureRecognizer property to your view controller:
#property UILongPressGestureRecognizer *gestureRecognizer;
You need to declare that your view controller conforms to the UITextViewDelegate and UIGestureRecognizerDelegate protocols:
#interface ViewController : UIViewController<UITextViewDelegate, UIGestureRecognizerDelegate>
In viewDidLoad:
self.textView.editable = NO;
self.textView.delegate = self;
self.gestureRecognizer = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:#selector(textViewLongPressed:)];
self.gestureRecognizer.delegate = self;
[self.textView addGestureRecognizer:self.gr];
This is the method that will be called when you long press the text view:
-(void) textViewLongPressed:(UILongPressGestureRecognizer *)sender
{
self.textView.editable = YES;
[self.textView becomeFirstResponder];
}
Implement this method from the UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
if (self.gestureRecognizer == gestureRecognizer){
return YES;
}
return NO;
}
When you finish editing the text view
-(void) textViewDidEndEditing:(UITextView *)textView
{
self.textView.editable = NO;
}
To dismiss the keyboard when you press return:
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if ([text isEqualToString:#"\n"])
[textView resignFirstResponder]; // or [textView endEditing:YES]
return YES;
}

Why is controlTextDidEndEditing being called when I send setEditable?

//1:
-(void) TextFieldEdit:(CDTextField *)textField{
[textField setEditable:YES];
}
//2:
- (void)controlTextDidEndEditing:(NSNotification *)aNotification{
NSTextField* textField = (NSTextField *)[aNotification object];
[textField setEditable:NO];
}
I hope setEditable at time 1, and close it at time 2.
But I found when I sent setEditable Xcode immediately called the controlTextDidEndEditing:.
Why?
EDIT: The first method is invoked via the following sub-classed method:
-(void)mouseDown:(NSEvent *)event {
if ([event type]==1)
{
NSInteger key=[event modifierFlags];
if ( key & NSCommandKeyMask)
{[self.delegate CDTextFieldEdit:self]; }
else
if (event.clickCount >1)
{[self.delegate CDTextFieldClicked:self]; return;}
}
[super mouseDown:event];
}

Xcode keyboard issue on scroll view

I have a scroll view on top single view. I have some textfields and UIPickers on it. Now I know how to make a keyboard go off when return is pushed. But, I am trying to get the keyboard off from textfield when the background is tapped or UIpicker is selected. I tried doing this...
Interface :
- (IBAction)textFieldReturn:(id)sender;
- (IBAction)backgroundTouched:(id)sender;
Implementation :
-(IBAction)textFieldReturn:(id)sender
{
[sender resignFirstResponder];
}
-(IBAction)backgroundTouched:(id)sender
{
[textField resignFirstResponder];
}
But the problem is I cant make sroll view as control type to make it work..
Try like this may be it helps you but not sure,
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
if (![[touch view] isKindOfClass:[UITextField class]]) {
[yourtextfield resignFirstResponder];
}
}
And for getting touch event on scrollview you have to take geasture recognization,
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleTap)];
[scroll addGestureRecognizer:singleTap];
-(void)singleTap{
[text resignFirstResponder];
//write whatever you want it.
}

How to dismiss a UIPicker that is connected to a UITextField

I have a UITextField, and when pushed a UIPickerView comes up to choose a value. How do I get the UIPickerView to dismiss once a value is chosen. Someone in another thread told me to resignFirstResponder the textfield, but my code isn't working. Any ideas? NOTE: I have two text fields and UI Pickers, that's why I have the 'if' 'else' statement.
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (textField == self.ageTextField)
{
[ageTextField resignFirstResponder];
[agePickerView removeFromSuperview];
return YES;
}
else
{
[relationshipTextField resignFirstResponder];
[relationshipPickerView removeFromSuperview];
return YES;
}
}
Try the following code which may solve your problem.
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (textField == self.ageTextField)
{
[ageTextField resignFirstResponder];
}
else
{
[relationshipTextField resignFirstResponder];
}
[pickerView removeFromSuperview];
return YES;
}
or resign your keyboard on following method of UIPickerView.
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
Please try resigning the UITextField in UIPickerViewDelegate method as follows.
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
[self.ageTextField resignFirstResponder];
pickerView.hidden = YES; //Show the pickerview again when you neeed it
}
Implement the UIPickerViewDelegate protocol and implement the following method:
- (void) pickerView: (UIPickerView*) pickerView
didSelectRow: (NSInteger) row
inComponent: (NSInteger) component
{
[self.ageTextField resignFirstResponder];
}