The keyboard will not be canceled in the application

In my application that I create, I have 3 UITextFields and one UITextView. My keyboard will appear for both of them, but I can’t get her to leave. I looked at some methods in Stack Overflow, but I cannot implement them correctly. Anyone want to tell me what I'm doing wrong in my next lines of code?

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;
    }

}
+4
source share
2 answers

Why is all this code used in your method viewDidLoad? It should be:

.h file:

@interface ViewController : UIViewController <UITextViewDelegate, UITextFieldDelegate>

// Other stuff here...

.m file:

- (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;
}
+4
source

I think you forgot to associate the UITextField delegate with the file owner.

0

All Articles