【问题标题】:Problem showing/hiding keyboard in iPhone appiPhone应用程序中显示/隐藏键盘的问题
【发布时间】:2011-04-01 06:34:52
【问题描述】:

在我的 iPhone 应用中,我遇到了一些与键盘显示/隐藏行为相关的问题。

我有三个文本字段;单击第三个文本字段时,我想显示 UIPickerView 并隐藏该文本字段的键盘。我能做到的。

现在的问题是,如果第一个或第二个文本字段的键盘是可见的,并且我点击第三个文本字段,选择器变得可见,但它出现在键盘后面(它只在键盘后面第一个或第二个文本字段)。

那么我应该怎么做才能使选择器本身可见而不显示任何键盘呢?

这里是代码:-

-(void) textFieldDidBeginEditing:(UITextField *)textField{

if (textField==thirdTextField) {

    [scroll setFrame:CGRectMake(00, 48, 320, 160)];
    [scroll setContentSize:CGSizeMake(320,335)];        
    [picker setHidden:NO];
    [tool1 setFrame:CGRectMake(0,180,320,44)];
    [tool1 setHidden:NO];
    [self.picker reloadAllComponents];

    [firtTextField resignFirstResponder];
    [secondTextField resignFirstResponder];
    [thirdTextField resignFirstResponder];
    }
else {  
      [scroll setFrame:CGRectMake(00, 48, 320, 200)];
      [scroll setContentSize:CGSizeMake(320,335)];
      [tool1 setHidden:NO];
      [tool1 setFrame:CGRectMake(0,220,320,44)];
}
}

问题是这样的

【问题讨论】:

    标签: iphone objective-c cocoa ios4


    【解决方案1】:

    保留三个文本字段作为控制器的成员。

    - (void)textFieldDidBeginEditing:(UITextField *)textField { 
      if(textField == 3rdTextField){
        [self.firstTextField resignFirstResponder];
        [self.secondTextField resignFirstResponder];
        [self.thirdTextField resignFirstResponder];
      }
    }
    
    - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
            if(textField==3rdTextField){
                [firstTextField resignFirstResponder];
                [secondTextField resignFirstResponder];
             }
            else if(textField==secondTextField){
                [firstTextField resignFirstResponder];
                [3rdTextField resignFirstResponder];
             }
            else if(textField==firstTextField){
                [secondTextField resignFirstResponder];
                [3rdTextField resignFirstResponder];
             }
    
           return YES;
     }
    

    希望这会对你有所帮助。

    【讨论】:

    • 感谢您的回复,但这不起作用我无法在我的pickerview前面隐藏键盘
    【解决方案2】:

    打电话

    [yourTextField resignFirstResponder]
    

    在所有其他文本字段上使其键盘消失。

    【讨论】:

      【解决方案3】:

      使用 resignFirstResponder 方法和文本字段。 [textField resignFirstResponder] 将隐藏键盘。

      【讨论】:

      • 我在回答的时候没有看到他的回复。对不起。
      【解决方案4】:

      然后在键盘可见时使用通知,并具有一个名为Ispickermisible的布尔值。 在keyboardDidShow 方法中检查picker 是否可见。如果它可见,则将其隐藏。
      添加通知:

      [[NSNotificationCenter defaultCenter] addObserver:self
      selector:@selector(keyboardDidShow:)
      name:UIKeyboardWillShowNotification
      object:nil];
      而且方法...
      - (void)keyboardDidShow:(NSNotification*)notif {
          if(isPickerVisible) {
              [self hidePicker];
          }   
      }
      

      希望这会有所帮助...

      【讨论】:

      • -1:OP 说该部分已经在工作:...显示 UIPickerView 并隐藏该文本字段的键盘。我能做到的。
      【解决方案5】:

      In - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField 方法对除第三个 textFied 之外的所有文本字段使用 resignFirstResponder:-

      - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
      {
              if(textField==thirdTextField)
              {
                  [firstTextField resignFirstResponder];
                  [secondTextField resignFirstResponder];
                  [textField resignFirstResponder];
      
                  [self showPickerView];
               }
             return YES;
       }
      

      【讨论】:

        猜你喜欢
        • 2016-03-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-28
        相关资源
        最近更新 更多