【问题标题】:Detect UITextField Lost Focus检测 UITextField 失去焦点
【发布时间】:2011-09-16 05:16:14
【问题描述】:

我搜索了很多谷歌和heree,但没有任何用处。

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

这里是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!为什么?!?

如何检测失去焦点?每次按下键盘按钮时都会调用此方法,而不仅仅是在最后!
有没有替代品?

编辑
我不想从文本切换,但我想在我点击屏幕时检测到失去焦点。 (键盘将关闭或不关闭,并且文本字段上不存在插入符号)!

谢谢。

【问题讨论】:

    标签: iphone objective-c ipad uitextfield protocols


    【解决方案1】:

    要处理点击外部文本字段,您可以在视图控制器中覆盖 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
        }
    
        ...
    }
    

    【讨论】:

    • 你可以在textFieldShouldBeginEditing查看第一响应者来处理:
    • 这不是很可靠.. touchesBegan 不会因为某种原因每次触摸屏幕上的某些东西时被调用
    【解决方案2】:
     - (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 中逐步设置标签,例如:0,1,2,3....等

    【讨论】:

    • 不,不,不正确!!这样,当我在不使用 ShouldReturn 的情况下单击文本字段外部时,我无法失去焦点!
    【解决方案3】:

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

    这是一个使用“完成”和“取消”按钮向键盘添加工具栏的类。我现在在 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
    

    【讨论】:

      【解决方案4】:

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

      #define kSomeTag 100
      textField.tag = kSomeTag;
      

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

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

      【讨论】:

      • 不,不起作用,因为当我单击文本字段外部时,没有任何变化!标签一样,无所谓!!!
      猜你喜欢
      • 2022-09-23
      • 1970-01-01
      • 2012-05-27
      • 1970-01-01
      • 2016-04-17
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      • 2016-09-23
      相关资源
      最近更新 更多