ios - 单击按钮时隐藏键盘?

标签 ios objective-c cocoa-touch uitextfield uitextfielddelegate

我正在创建如下所示的 iPhone 应用程序。

在屏幕的末尾我有文本字段。我为此添加了一个代表。因为它是数字键盘,所以我单独添加了按钮,以便在单击按钮时隐藏键盘。

下面是我的代码:

.h

@interface SearchViewController : UIViewController<UITextFieldDelegate>

@property (retain, nonatomic) IBOutlet UITextField *textField006;
@property (retain, nonatomic) IBOutlet UIButton *doneButton;
- (IBAction)doneAction:(id)sender;

.m

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    NSLog(@"textFieldShouldReturn");
    return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    NSLog(@"textFieldDidBeginEditing");
    // Ensure the relevant text field is visible
    CGAffineTransform translation = CGAffineTransformIdentity;
    CGRect screenBound = [[UIScreen mainScreen] bounds];
    CGSize screenSize = screenBound.size;
    CGFloat screenHeight = screenSize.height;

    if (screenHeight==480 || screenHeight==568) {
            translation = CGAffineTransformMakeTranslation(0, -120);
        doneButton.hidden = NO;
        NSLog(@"line 3");
        [UIView beginAnimations:nil context:nil];
        self.view.transform = translation;
        [UIView commitAnimations];
    }
}

- (IBAction)doneAction:(id)sender {
    doneButton.hidden = NO;    
    doneButton.hidden = YES;
    [textField006 resignFirstResponder];
    [UIView beginAnimations:nil context:nil];
    self.view.transform = CGAffineTransformIdentity;
    [UIView commitAnimations];
    [self.textField006 resignFirstResponder];

}

为什么键盘不隐藏?我怎样才能隐藏它?

Keyboard == Decimal Pad Return key >> Go Auto-enable Return key = Ticked

最佳答案

一定要用endEditing:如果它当前没有正确隐藏。

关于endEditing:

"endEditing causes the view (or one of its embedded text fields) to resign the first responder status."

"This method looks at the current view and its subview hierarchy for the text field that is currently the first responder. If it finds one, it asks that text field to resign as first responder. If the force parameter is set to YES, the text field is never even asked; it is forced to resign."

因此,以下应该有效(在您的按钮点击操作方法内):

[self.view endEditing:YES];

关于ios - 单击按钮时隐藏键盘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15521508/

相关文章:

ios - UIView 不从底部向上滑动

objective-c - 是否可以有 2 个不同的 NSNumber 对象具有相同的值?

swift - XCTestCase 中未调用 UITextFieldDelegate

iOS RxSwift 如何检查 Result == .success?

ios - 如何在应用程序委托(delegate)中将选项卡 Controller 设置为根 Controller 而不实例化(重新加载)它?

ios - 在Windows中无法以编程方式在iOS中录制的音频无法播放

ios:文本在 iPhone 5S 及更早版本(4 英寸及更小的设备)上无法正确换行

ios - 具有不同数组的多个 TableView

ios - 如何在 SpriteKit 中使用 UITextView?

ios - 展开到最后一个 View Controller