Detect UITextField Lost Focus
i searched a lot googling and heree, but nothing useful.
I have two textfields and i don't able to recognize which one lost the focus.
I tried all options, but nothing.Here the textFieldDidEndEditing
:
- (void) textFieldDidEndEditing:(UITextField *)textField {
NSLog(@"%@", [textField state]);
NSLog(@"%d", [textField isSelected]);
NSLog(@"%d", [textField isFirstResponder]);
NSLog(@"%d", [textField isHighlighted]);
NSLog(@"%d", [textField isTouchInside]);
if ( ![textField isFirstResponder] || ![textField isSelected] ) {
//if ( [textField state] != UIControlStateSelected) {
NSLog(@"not selected!");
[...]
// remove view / etc...
}
}
All NSLog returns 0! Why?!?
How can i detect lost focus? This method has called every time that i press a keyboard button, non only at the end!
Is there any alternatives?EDIT:
I don't want to switch from texts but i want to detect lost focus when i click anywa开发者_StackOverflow社区ys on screen. (the keyboard will dismiss or not and the caret is not present on textfield)!thanks.
To handle tapping outside text fields you can override touchesBegan
in your view controller:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *) event
{
UITouch *touch = [[event allTouches] anyObject];
if ([textField1 isFirstResponder] && (textField1 != touch.view))
{
// textField1 lost focus
}
if ([textField2 isFirstResponder] && (textField2 != touch.view))
{
// textField2 lost focus
}
...
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
NSLog(@"%d",textFiled.tag);
NSInteger nextTag = textField.tag + 1;
UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
if (nextResponder) {
[nextResponder becomeFirstResponder];
} else {
[textField resignFirstResponder];
}
return YES;
}
The UITextField with tag had lost focus in textFieldShouldReturn method
This will help you to go from one TextField to another....just set tag incremently in all TextFields ex : 0,1,2,3....etc
This isn't a direct answer since you asked about how to handle when losing focus. I think there are times when it's nice to have explicit save and cancel buttons to dismiss. Especially on a text view where you want to preserve the return key for it's intended use.
This is a class which adds a toolbar to the keyboard with "Done" and "Cancel" buttons. I have this working in iOS 8 right now. I am pretty new to iOS so there might be better ways to do this. Always open to suggestions on how to improve.
DismissableTextView.h...
#import <UIKit/UIKit.h>
@interface DismissableTextView : UITextView
@end
DismissableTextView.m...
#import "DismissableTextView.h"
@implementation DismissableTextView
- (instancetype)init
{
self = [super init];
if (self) {
[self setInputView];
}
return self;
}
- (id) initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
[self setInputView];
}
return self;
}
- (void)awakeFromNib
{
[super awakeFromNib];
[self setInputView];
}
- (void) setInputView {
[self createToolbar];
}
-(void) createToolbar {
// Create toolbar for the keyboard so it can be dismissed...
UIToolbar* toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
toolbar.barStyle = UIBarStyleDefault;
toolbar.items = [NSArray arrayWithObjects:
[[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:self action:@selector(cancelClicked)],
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:@"Apply" style:UIBarButtonItemStyleDone target:self action:@selector(doneClicked)],
nil];
[toolbar sizeToFit];
self.inputAccessoryView = toolbar;
}
- (IBAction)didBeginEditDescription:(id)sender
{
}
-(void)cancelClicked{
// respond to cancel click in the toolbar
[self resignFirstResponder];
}
-(void)doneClicked{
// respond to done click in the toolbar
[self resignFirstResponder];
}
@end
When you are creating the text fields, assign different tags to them:
#define kSomeTag 100
textField.tag = kSomeTag;
In your - (void)textFieldDidEndEditing:(UITextField *)textField method, you can tell which textfield ended editing by querying its tag:
if (textField.tag == kSomeTag) {
// do something
}
精彩评论