在我的应用程序中,我有3个UITextFields和一个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;
}
}
答案 0 :(得分:4)
为什么所有代码都在您的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;
}
答案 1 :(得分:0)
我想您忘了将UITextField的委托连接到文件所有者。