2015-07-22 79 views
0

我已经使用两个delegate方法为textView创建占位符。在textView中占位符的替代

代码:

-(BOOL)textViewShouldBeginEditing:(UITextView *)textView 
{ 

    if([Description.text isEqualToString:@"Description of home"]&&[Description.font isEqual:[UIFont fontWithName:@"Helvetica" size:18]]) 
    { 
     Description.text=nil; 
    } 
    if([Display.text isEqualToString:@"Display"]&&[Display.font isEqual:[UIFont fontWithName:@"Helvetica" size:18]]) 
    { 
     Display.text=nil; 
    } 
    return YES; 
} 
-(BOOL)textViewShouldEndEditing:(UITextView *)textView 
{ 

    if ([Description.text isEqualToString:@""]) // Description is textViewObject 
    { 
     [email protected]"Description of home"; 
     Description.font=[UIFont fontWithName:@"Helvetica" size:18]; 
    } 


    if ([Display.text isEqualToString:@""]) 
    { 
     [email protected]"Display"; // Display is textViewObject 
     Display.font=[UIFont fontWithName:@"Helvetica" size:18]; 
    } 
    return YES; 
} 
@end 

这是工作正确的。但问题是当我点击任何textView时,文本从所有textView获取nil。这是因为我已将所有textView连接到委托。

我希望文本应该只在特定的textView上消失。

+0

的[占位符的UITextView(http://stackoverflow.com/questions/1328638/placeholder-in-uitextview) –

+0

可能重复我认为占位符支持添加到UITextView中的最佳方式是它的子类,像这样:[YXTextView](https://github.com/yixia-team/YXTextView)。 – Allen

+0

您需要继承UITextView并将标签添加为子视图。通过委托方法,你会让自己变得疯狂。 – DBoyer

回答

2

,你可以在委托方法像这样添加条件:

-(BOOL)textViewShouldBeginEditing:(UITextView *)textView 
{ 
if(textView == yourTextView1) 
{ 
    //your code for perticular textView1 
} 
else if(textView == yourTextView2) 
{ 
    //your code for perticular textView2 
} 

    return YES; 
} 
-1

检查如果委托在想UITextView操作:

-(BOOL)textViewShouldBeginEditing:(UITextView *)textView 
{ 

    if(textView == Description && [Description.text isEqualToString:@"Description of home"]&&[Description.font isEqual:[UIFont fontWithName:@"Helvetica" size:18]]) 
    { 
     Description.text=nil; 
    } 
    if(textView==Display && [Display.text isEqualToString:@"Display"]&&[Display.font isEqual:[UIFont fontWithName:@"Helvetica" size:18]]) 
    { 
     Display.text=nil; 
    } 
    return YES; 
} 
-1
通过@ CmKndy对于这个问题

最简单的方法是https://stackoverflow.com/a/10201671/3633534

使用UITextViewDelegate代表在您的班级中并添加文本@"placeholder text here..."textViewlightGrayColor默认显示。

下面的代码是占位符在textView

- (void)textViewDidBeginEditing:(UITextView *)textView 
{ 
    if ([textView.text isEqualToString:@"placeholder text here..."]) { 
     textView.text = @""; 
     textView.textColor = [UIColor blackColor]; //optional 
    } 
    [textView becomeFirstResponder]; 
} 

- (void)textViewDidEndEditing:(UITextView *)textView 
{ 
    if ([textView.text isEqualToString:@""]) { 
     textView.text = @"placeholder text here..."; 
     textView.textColor = [UIColor lightGrayColor]; //optional 
    } 
    [textView resignFirstResponder]; 
} 

对于你的问题[更新]

-(BOOL)textViewShouldBeginEditing:(UITextView *)textView 
{ 
    if(textView == Description && [Description.text isEqualToString:@"Description of home"]) 
    { 
     Description.text = @""; 
    } 
    else if(textView == Display && [Display.text isEqualToString:@"Display"]) 
    { 
     Display.text = @""; 
    } 
    return YES; 
} 

-(BOOL)textViewShouldEndEditing:(UITextView *)textView 
{ 
    if (textView == Description && [Description.text isEqualToString:@""]) // Description is textViewObject 
    { 
     Description.text = @"Description of home"; 
     Description.font=[UIFont fontWithName:@"Helvetica" size:18]; 
    } 
    else if (textView == Display && [Display.text isEqualToString:@""]) 
    { 
     Display.text = @"Display"; // Display is textViewObject 
     Display.font=[UIFont fontWithName:@"Helvetica" size:18]; 
    } 
    return YES; 
} 
@end 
+0

你可以做到这一点,但它是非常规的。 创建一个引用UITextView的IBOutlet属性比匹配字符串更安全,更具可预测性。您可以在“if语句”或IB的占位符字段中键入错误的内容,并且可能需要更多时间才能进行调试。 – Adama

+1

@Adama,'(UITextView *)textView'将始终引用当前的'textView',并且作用域仅在定义的方法中,因此它们不会出错。 'IBOutlet'是不同的东西,它是全球使用的任何'id'。 – Sujay

0

您需要区分每个UITextViews以某种方式。

如果屏幕上只有一小部分UITextViews,则应将每个UItextView保存为一个属性。看看这个YouTube视频creating IBOutlets

然后,只需命名为了UITextViews:textView1,textView2等等

然后在你的委托方法,您可以检查是否在法传递TextView的等于您要从中删除文本的属性。

if (textView == self.textView1) { 
    //delete text 
} else { 
    //do nothing or something else 
}