UITextView委托方法 [英] UITextView delegate methods
问题描述
我正在尝试使委托方法与UITextView一起使用,但是由于某些原因它不起作用.
I am trying to get delegate methods to work with UITextView, but it's not working for some reason.
我在viewController.h中声明它是一个UITextViewDelegate
I have declared in my viewController.h that it is a UITextViewDelegate
当我点击textView时,我试图使以下代码起作用,以擦除默认代码"TEXT".
I am trying to get the following code to work to erase the default code "TEXT" when I tap on the textView.
- (void)textViewDidBeginEditing:(UITextView *)textView {
if (myTextView.text == @"TEXT") {
[myTextView setText:@""];
}
NSLog(@"did begin editing");
}
当我点击textView并出现键盘时,我希望清除文本并看到NSLog打印.什么都没发生
I expected to the text to be cleared and to see the NSLog print when I tap on the textView and the keyboard appears. Nothing at all happens
顺便使用文本视图是因为我需要根据其内容大小缩放视图,并且似乎textView具有contentSize属性,而whit标签和textField没有.
Using a text view by the way because I need to scale the view based on its content size and seems that the textView has a contentSize property, whit label and textField do not.
更新:
我应该使用过:
if ([myTextView.text isEqualToString:@"TEXT"]) {
[myTextView setText:@""]; }
推荐答案
您的 Test2ViewController.m 文件中缺少此方法:
this method is missing from your Test2ViewController.m file:
- (void)viewDidLoad {
[myTextView setDelegate:self];
}
,或者,如果您更喜欢这种方式,也可以在 Interface Builder 中连接该委托.
or you can connect the delegate in the Interface Builder as well, if you prefer that way better.
更新#1:
将此方法添加到您的类中以控制return
键.
add this method to you class for controlling the return
key.
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if ([text isEqualToString:@"\n"]) {
NSLog(@"Return pressed, do whatever you like here");
return NO; // or true, whetever you's like
}
return YES;
}
这篇关于UITextView委托方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!