检测 UITextField 失去焦点

发布于 2024-11-15 20:21:24 字数 817 浏览 3 评论 0原文

我在谷歌上搜索了很多,但没有什么用处。

我有两个文本字段,但我无法识别哪个文本字段失去了焦点。
我尝试了所有选项,但没有任何结果。

这里的 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...
  }
}

所有 NSLog 返回 0!为什么?!?

如何检测失去焦点?每次我按下键盘按钮时都会调用此方法,不仅仅是在最后!
还有其他选择吗?

编辑
我不想从文本切换,但我想在我单击屏幕上的任何内容时检测到失去焦点。 (键盘是否会关闭,并且文本字段中不存在插入符号)!

谢谢。

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 anyways on screen. (the keyboard will dismiss or not and the caret is not present on textfield)!

thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

那伤。 2024-11-22 20:21:24

要处理点击外部文本字段,您可以在视图控制器中覆盖 touchesBegan

- (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
    }

    ...
}

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
    }

    ...
}
潇烟暮雨 2024-11-22 20:21:24
 - (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;
  }

带有标签的 UITextField 在 textFieldShouldReturn 方法中失去了焦点

这将帮助您从一个 TextField 转到另一个 TextField...只需在所有 TextField 中递增设置标签,例如:0,1,2,3...等

 - (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

软的没边 2024-11-22 20:21:24

这不是直接答案,因为您询问了失去焦点时如何处理。我认为有时候有明确的保存和取消按钮来关闭是很好的。特别是在文本视图中,您希望保留返回键以供其预期用途。

该类向键盘添加了一个带有“完成”和“取消”按钮的工具栏。我现在可以在 iOS 8 中使用它。我对 iOS 还很陌生,所以可能有更好的方法来做到这一点。始终乐于接受有关如何改进的建议。

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

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
生生漫 2024-11-22 20:21:24

创建文本字段时,为它们分配不同的标签:

#define kSomeTag 100
textField.tag = kSomeTag;

在 - (void)textFieldDidEndEditing:(UITextField *)textField 方法中,您可以通过查询其标签来判断哪个文本字段结束了编辑:

if (textField.tag == kSomeTag) {
    // do something
}

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文