【问题标题】:Keyboard is not resigning键盘不辞职
【发布时间】:2013-03-07 10:06:11
【问题描述】:

很抱歉在这里问了这么愚蠢的问题。

实际上,我有一个文本字段列表,通过单击下一步按钮,我将在其中移动到下一个文本字段。 它工作正常,但在那个文本字段中,一个用于日期字段,为此我使用日期选择器作为输入访问器。

当我直接单击该文本字段时,它的工作正常并且日期选择器出现。 但是当我用下一个按钮键盘进入那个文本字段时,我的文本字段被隐藏了。

对于日期选择器显示,我使用 textFieldDidBegin 方法。我试过使用 [文本域 resignFirstResponder]; 和 [datePicker becomeFirstResponder]; 但没有什么对我有用。

任何帮助将不胜感激。

谢谢。

【问题讨论】:

标签: iphone ios datepicker textfield resignfirstresponder


【解决方案1】:

您应该使用textField.inputView 属性来显示日期选择器。只有在这种情况下[textfield resignFirstResponder]; 才能正常工作。

- (void)viewDidLoad {
  ...
  // Assume that self.datePicker contains configured date picker view
  // With added target on UIControlEventValueChanged action
  textField.inputView = self.datePicker;
  ...
}

附:您应该只发送 becomeFirstResponder 文本字段视图,而不是日期选择器。日期选择器只是输入视图。

【讨论】:

    【解决方案2】:

    几天前我也遇到了同样的问题,我为此场景编写了以下代码,首先我禁用了dataTextfield 上的用户交互,但我已经触及了完整的UITableCell。我覆盖了简单的UITouch 事件方法而不是 textbegin 委托方法。请参阅我的以下代码。

    在tableviewController.h文件中

     #import <UIKit/UIKit.h>
     #import "CustomTableCell.h"
    @class CustomTableCell;
    @interface PersonalInfoTableViewController : UITableViewController<CustomTableCellDelegate>{
    @property(nonatomic, strong) UITextField *previousTextField;
    @end
    
    In tableviewController.m file 
    
     @implementation TableViewController
     @synthesize previousTextField;
    //When you create custom table cell set your CustomcellDelegate = self in tableView:cellForRowAtIndexPath method\
     //also assign previousTextField to CustomTableCell textfield
    
    -(void)tableViewTouch{
    [previousTextField resignFirstResponder];
    }
    
     in CustomTableCell.h file
    #import <UIKit/UIKit.h>
     @protocol CustomTableCellDelegate
     @optional
       -(void)tableViewTouch;
     @end
    @interface CustomTableCell : UITableViewCell<UITextFieldDelegate>
    
    @property(nonatomic, unsafe_unretained) id<CustomTableCellDelegate> delegate;
    @property(nonatomic, weak) IBOutlet UITextFiled *theCellTextField;
    @end
    
    in CustomTableCell.m file
    @synthesize theCellTextField, delegate;
      -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
     //Here my other method to show datepicker in popupViewController on tablecell.
      [delegate tableViewTouch];
      }
    

    这是仅适用于 ARC 的代码

    【讨论】:

      【解决方案3】:

      为您的文本字段赋予标签值,然后

      - (void)textFieldDidBeginEditing:(UITextField *)textField
      {
          if (textField.tag==6) {
      
              [textField resignFirstResponder];
      
              //Show your picker
          }
      }
      

      【讨论】:

        【解决方案4】:

        为 datePicker 容器提供每个 UITextField 的标签 带有标签 102 的文本字段

        -(void)textFieldDidBeginEditing:(UITextField *)sender
        {
        
            if ([sender tag]==102)
            {
                [self.textFieldDatePiker resignFirstResponder];
                [self.alaramTime resignFirstResponder];
                 .
                 .
                 . 
                 //Write all textField with resignFirstResponder
        
                [self showPickerView];
            }
        
        }
        

        【讨论】:

          【解决方案5】:

          UITextFieldtextFieldDidBegin 方法在textField 变为first responder 时得到called

          所以使用textFieldShouldBeginEditingnot allow textfield editing。还有set tag 代表textfieldneed date picker

           - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
           {
               if([textfield tag] == some tag no)
                  [self.view endEditing:YES]; // in case if any textfiled is first responder before  date picker to open
                  [datePicker becomeFirstResponder]; //open date picker
                  return NO; // not edit textfield
               else 
                  return YES; // edit textfield here for other case
           }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2012-02-10
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-08-31
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多