键盘不会在应用程序中关闭

Posted

技术标签:

【中文标题】键盘不会在应用程序中关闭【英文标题】:Keyboard won't dismiss in app 【发布时间】:2013-10-15 02:13:08 【问题描述】:

在我正在制作的应用程序中,我有 3 个 UITextField 和一个 UITextView。对于他们两个,我的键盘都会出现,但我无法让它消失。我在 Stack Overflow 上查找了一些方法,但似乎无法以正确的方式实现它们。有人愿意告诉我我在以下代码行中做错了什么吗?

ViewController.h

@interface ViewController : UIViewController <UITextViewDelegate>

@property (strong, nonatomic) NSString *dna;
@property (weak, nonatomic) IBOutlet UITextField *dnaOut;
@property (weak, nonatomic) IBOutlet UITextField *mrnaOut;
@property (weak, nonatomic) IBOutlet UITextField *trnaOut;
@property (weak, nonatomic) IBOutlet UITextView *aminoOut;
- (IBAction)translateButton:(UIButton *)sender;
- (IBAction)clearButton:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet UILabel *dnaError;
@property (weak, nonatomic) IBOutlet UILabel *mrnaError;
@property (weak, nonatomic) IBOutlet UILabel *trnaError;
@property (weak, nonatomic) IBOutlet UISegmentedControl *inputType;



@end

ViewController.m

- (void)viewDidLoad

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    _aminoOut.delegate = self;


    -(BOOL) _aminoOut textFieldShouldReturn:(UITextField *)textfield 
        [textField resignFirstResponder];
        return YES;
    


    - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range
replacementText:(NSString *)text
    

        if ([text isEqualToString:@"\n"]) 

            [textView resignFirstResponder];
            // Return FALSE so that the final '\n' character doesn't get added
            return NO;
        
        // For any other character return TRUE so that the text gets added to the view
        return YES;
    


【问题讨论】:

你设置了 UITextFields 的代理了吗? 【参考方案1】:

为什么所有这些代码都在您的 viewDidLoad 方法中?应该是:

.h 文件:

@interface ViewController : UIViewController <UITextViewDelegate, UITextFieldDelegate>

// Other stuff here...

.m 文件:

- (void)viewDidLoad

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.dnaOut.delegate = self;
    self.mrnaOut.delegate = self;
    self.trnaOut.delegate = self;
    self.aminoOut.delegate = self;



- (BOOL)textFieldShouldReturn:(UITextField *)textfield 
    [textField resignFirstResponder];
    return YES;


- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text

    if ([text isEqualToString:@"\n"]) 

        [textView resignFirstResponder];
        // Return FALSE so that the final '\n' character doesn't get added
        return NO;
    
    // For any other character return TRUE so that the text gets added to the view
    return YES;

【讨论】:

【参考方案2】:

我认为您忘记将 UITextField 的委托连接到文件所有者。

【讨论】:

在Interface Builder中右击你的UITextField,你会看到“delegate”和一个圆圈,把那个圆圈拖到文件所有者那里。

以上是关于键盘不会在应用程序中关闭的主要内容,如果未能解决你的问题,请参考以下文章

在 React Native 中关闭多行 TextInput 中的键盘

发出警报后,我无法在我的反应本机应用程序中关闭我的键盘

在 UIViewController 中关闭键盘

如何在 Android SearchView 中关闭键盘?

如何在本机反应中关闭系统键盘

在动态 UITableView 中关闭 iPhone 键盘