Detect UITextField Lost Focus

心已入冬 提交于 2019-11-30 05:02:13

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
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!