【问题标题】:Hiding Keyboard in iphone resingFirstResponder Not working在 iphone resignFirstResponder 中隐藏键盘不起作用
【发布时间】:2011-12-17 10:19:39
【问题描述】:

我创建了带有警报视图的 UITextField。在 UITextField 上键入后,我想在用户单击“完成”按钮时隐藏键盘。

这是我的代码。当我运行它时,它正在使用 hideKeyBoard 方法(我发现使用 NSLog 和调试)。但是这段代码没有隐藏键盘。请帮我。

- (IBAction)doAlertInput:(id)sender
{
    UIAlertView *alertDialog;

    alertDialog = [[UIAlertView alloc] 
                   initWithTitle: @"Please Enter Your Email Address!" message:@"You won’t see me" 
                   delegate: self cancelButtonTitle: @"Ok" otherButtonTitles: nil];

    userInput=[[UITextField alloc] initWithFrame: CGRectMake(12.0, 70.0, 260.0, 25.0)];

    userInput.returnKeyType = UIReturnKeyDone;

    [userInput addTarget:self action:@selector(hideKeyBoard) forControlEvents:UIControlEventEditingDidEndOnExit];

    [userInput setBackgroundColor:[UIColor whiteColor]];

    [alertDialog addSubview:userInput];

    [alertDialog show];
    [alertDialog release];
}

-(void)hideKeyBoard
{
    [userInput resignFirstResponder];
    NSLog(@"Key Board Hid");
}

【问题讨论】:

    标签: iphone keyboard uitextfield hide uialertview


    【解决方案1】:

    我认为您缺少 UITextField 的委托

    userInput.delegate = self;

    还实现了协议,应该就是这样。干杯。

    【讨论】:

      【解决方案2】:

      显示键盘后,它会覆盖警报视图。所以我正在做的是将我的警报视图提高一点。那么这将不是问题,因为键盘不再覆盖警报视图。

      CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0.0, 130.0);
      [myAlertView setTransform:myTransform];
      

      CGAffineTransformMakeTranslation() 的第一个参数是将视图原点移动到的 X 坐标。第二个参数是将视图原点移动到的 Y 位置。 130.0 并没有将警报视图完全居中在状态栏和键盘之间。

      您需要在 UIAlertView 实例化和 [UIAlertView show] 行之间的某处添加这两行。我把它们直接放在表演线之前。

      还将 CoreGraphics 框架添加到您的项目中。

      我认为这会对您有所帮助。谢谢你:)

      【讨论】:

        【解决方案3】:

        你也可以使用这个方法。

        在你的 .h 文件中首先设置委托方法

        UITextfieldDelegate 使用

        然后在您的 .m 文件中。

        -(BOOL)textFieldShouldReturn:(UITextField *)textField {

          [textField resignFirstResponder];
        
          return YES;
        

        }

        确保不要忘记添加文本字段委托,否则方法 -(BOOL) 将不会出现。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-02-17
          • 1970-01-01
          • 2016-11-12
          • 2011-03-21
          • 2011-03-28
          相关资源
          最近更新 更多