【问题标题】:Hide keyboard/ resignFirstResponder forcibly强制隐藏键盘/ resignFirstResponder
【发布时间】:2011-11-25 13:34:44
【问题描述】:

我正在开发一个应用程序,该应用程序在其每个单元格的右侧有一个 tableView 和一个 textField(有超过 20 个单元格)。 除了最后一行之外,我已经为每一行创建了自定义单元格。 最后一行只有一个按钮。

现在我想在单击按钮时致电resignFirstResponder。
我该怎么办请帮忙?

【问题讨论】:

  • 所以,您在点击文本字段时获得了 k/b,而您想在该按钮点击时退出 k/b。?

标签: iphone objective-c cocoa-touch uitextfield uikeyboard


【解决方案1】:

您将必须跟踪哪个单元格中的哪个文本字段具有第一响应者并像这样辞职。

[myCellTextField resignFirstResponder];

【讨论】:

    【解决方案2】:

    您可能希望使用键盘跟踪文本字段。在您的控制器中实现<UITextFieldDelegate> 协议,并将控制器设置为每个文本字段的代表。像这样写textFieldDidBeginEditing:方法,设置一个名为currentTextField的实例变量:

    - (void)textFieldDidBeginEditing:(UITextField *)textField {
        currentTextField = [textField retain];
    }
    

    然后,在您的按钮操作中运行[currentTextField resignFirstResponder]。

    【讨论】:

      【解决方案3】:

      Aopsfan 的回答可能是目前为止最好的解决方案。但是,要添加它(因为我无法发布 cmets),请记住释放对象:

      - (void)textFieldDidBeginEditing:(UITextField *)textField {
          if (currentTextField != nil) {
              [currentTextField release];
          }
          currentTextField = [textField retain];
      }
      

      最好还是使用@property 和@synthesize,这样运行时就可以为您进行内存管理。

      [ViewController].h

      @property (nonatomic, retain) UITextField* currentTextField;
      

      [ViewController].m

      @synthesize currentTextField = _currentTextField;
      
      - (void)viewDidLoad|Appear {
          self.currentTextField = nil;
      }
      
      - (void) dealloc {
          [_currentTextField release], _currentTextField = nil;
          ...
          [super dealloc];
      }
      
      - (void)textFieldDidBeginEditing:(UITextField *)textField {
          self.currentTextField = textField;
      }
      
      - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
          if (self.currentTextField) {
              [self.currentTextField resignFirstResponder];
          }
      }
      

      【讨论】:

        【解决方案4】:

        我认为这个链接会对你有所帮助:

        Objective C: ResignFirstResponder with a button

        提供一些代码,这样会更容易帮助你。

        希望这对您有所帮助。 :)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-06-12
          • 2011-12-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-12-28
          相关资源
          最近更新 更多