2012-03-21 8 views
0

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

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

@end

答えて

0

私はtextViewDidChange:が編集が完了した後にのみ呼び出されると信じています。代わりにtextViewDidBeginEditing:にプレースホルダーを非表示にすることをお勧めします。

+0

あなたはそれが何を意味するか分かりません。 –

+0

あなたがペーストしたコードは、ユーザがUITextViewにテキストを入力している間は機能しません。 the-TextViewDidChange:(UITextView *)theTextView {...}は、別のフィールドを選択した後、またはキーボードを隠した後にのみ呼び出されます。 'textViewDidBeginEditing:'は、ユーザがそのフィールドに入力を開始すると直ちに呼び出されますUITextView。 – MechEthan

+0

それは理にかなっています。私はそれを行う別の方法を考え出す。私はちょうど 'UITextViewTextDidBeginEditingNotification'にオブザーバを追加し、プレースホルダを削除した関数を作成しました。 –

関連する問題