Reputation: 198
In my app I am making, I have 3 UITextFields and one UITextView. For both of them, my keyboard will appear, but then I cant get it to go away. I looked up some methods on Stack Overflow but I can't seem to implement them the right way. Would anyone care to tell me what I'm doing wrong in my following 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;
}
}
Upvotes: 4
Views: 1382
Reputation: 46713
Why is all of that code within your viewDidLoad
method? 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;
}
Upvotes: 4
Reputation: 2180
I think you forgot to connect the delegate of UITextField to files owner.
Upvotes: 0