Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在UITextView(iphone)中执行此操作_Iphone_Uitextview - Fatal编程技术网

如何在UITextView(iphone)中执行此操作

如何在UITextView(iphone)中执行此操作,iphone,uitextview,Iphone,Uitextview,在我的文本视图中,有一个默认值“First name” 我想在它里面实施这样的操作 1) 应首先显示“名字” 2) 当我开始编辑时,它应该变成零 3) 当我完成编辑后,如果该字段仍然为零,则应再次显示为“名字” 有人能帮我吗 //***这就是我现在正在做的***// - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)tex

在我的文本视图中,有一个默认值“First name”

我想在它里面实施这样的操作

1) 应首先显示“名字”
2) 当我开始编辑时,它应该变成零
3) 当我完成编辑后,如果该字段仍然为零,则应再次显示为“名字”

有人能帮我吗

//***这就是我现在正在做的***//

 - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range 
    replacementText:(NSString *)text
         {    
if (first_name.text=@"") {
    first_name.text=@"First Name(Required)";
    }
// Any new character added is passed in as the "text" parameter
if ([text isEqualToString:@"\n"]) {
    // Be sure to test for equality using the "isEqualToString" message

    [textView resignFirstResponder];

    // Return FALSE so that the final '\n' character doesn't get added
    return FALSE;
}
// For any other character return TRUE so that the text gets added to the view
return TRUE;
   return TRUE;
}


- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
NSLog(@"textViewShouldBeginEditing");
textView.scrollEnabled=NO;

if (textView == first_name) {
    first_name.text=nil;
}


return YES;

}

可以在中找到用于实现此概念的所有委托方法。

您需要在文本视图顶部覆盖一个UILabel,其中包含所需的文本,将其userInteractionEnabled设置为NO,并在文本字段中的委托方法shouldBeginEditing中,在返回YES之前隐藏UILabel,并在shouldFinishEditing中取消隐藏它

你是说一个超文本字段?有一个名为placeHolder的属性。正如esqew在下面所说的,使用委托方法textViewDidStart/EndEditingTT…我只是这样做了。。你能告诉我发生了什么事吗?因为这不会发生使用textViewDidBeginEditing方法并设置first_name.text=@;稍后使用TextViewDiEndediting并检查值,如果为空,请再次设置占位符。