0

ここには UITextView のプレースホルダーのリンクがたくさんあることを知っていますが、うまくいったものを試してみましたが、うまくいきません。textView に入力を開始しても、プレースホルダーのテキストはそのままです。誰かが理由を知りましたか?以下にコードを貼り付けました。

AddExerciseViewController.m

 #import "AddExerciseViewController.h"

 @implementation AddExerciseViewController
 @synthesize nameTextField = _nameTextField;
 @synthesize descriptionTextView = _descriptionTextView;
 @synthesize placeholderLabel = _placeholderLabel;

 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
 {
     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
     if (self) {     
     }
     return self;
 }

 - (void)viewDidLoad
 {
     _descriptionTextView.layer.borderWidth = 3.0f;
     _descriptionTextView.layer.borderColor = [[UIColor grayColor ] CGColor];
     _descriptionTextView.layer.cornerRadius = 5;
     _descriptionTextView.clipsToBounds = YES;

    _placeholderLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 0.0,           _descriptionTextView.frame.size.width - 20.0, 34.0)];
     [_placeholderLabel setText: @"FooBar"];

     // placeholderLabel is instance variable retained by view controller
     [_placeholderLabel setBackgroundColor:[UIColor clearColor]];
     [_placeholderLabel setTextColor:[UIColor lightGrayColor]];

     [_descriptionTextView addSubview:_placeholderLabel];
     [super viewDidLoad];
 }

 - (void) textViewDidChange:(UITextView *)theTextView
 {
     if(![_descriptionTextView hasText]) {
    [_descriptionTextView addSubview:_placeholderLabel];
    [UIView animateWithDuration:0.15 animations:^{
        _descriptionTextView.alpha = 1.0;
    }];
 } else if ([[_descriptionTextView subviews] containsObject:_placeholderLabel]) {

    [UIView animateWithDuration:0.15 animations:^{
        _placeholderLabel.alpha = 0.0;
    } completion:^(BOOL finished) {
        [_placeholderLabel removeFromSuperview];
    }];
}
 }


 - (void)textViewDidEndEditing:(UITextView *)theTextView
 {
     if (![_descriptionTextView hasText]) {
         [_descriptionTextView addSubview:_placeholderLabel];
         [UIView animateWithDuration:0.15 animations:^{
             _placeholderLabel.alpha = 1.0;
         }];
     }
 }

 - (void)viewDidUnload
 {
     [self setDescriptionTextView:nil];
     [self setNameTextField:nil];
     [super viewDidUnload];
 }

@終わり

4

1 に答える 1

0

textViewDidChange:編集が終わって初めて呼ばれると思います。代わりに、プレースホルダーを非表示にすることをお勧めしtextViewDidBeginEditing:ます。

于 2012-03-21T22:31:08.070 に答える