4

私が作成しているアプリには、3 つの UITextFields と 1 つの 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;
    }

}
4

2 に答える 2

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;
}
于 2013-10-15T02:28:36.963 に答える
0

UITextField のデリゲートをファイルの所有者に接続するのを忘れたと思います。

于 2013-10-15T02:21:38.680 に答える