UITextView委托从来没有调用过 [英] UITextView delegate never called

查看:96
本文介绍了UITextView委托从来没有调用过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UITextview委托 - (BOOL)textView:(UITextView *)textView ,从来没有得到调用,我一直在努力这个几天徒劳无功。这是我有的

I have a UITextview delegate - (BOOL)textView:(UITextView *)textView that is never getting called and I have been struggling with this for a couple of days to no avail. Here is what I have

我在我的头文件和ViewDidLoad中声明了< UITextViewDelegate> 设置委托,我尝试过 self.myTextView.delegate = self;
myTextView.delegate = self;

I have declared the <UITextViewDelegate> in my header file and in ViewDidLoad I am setting the delegate, I have tried self.myTextView.delegate = self; and myTextView.delegate = self; along with wiring up the delegate in the NIB file instead of declaring it programmatically.

我有几个自定义按钮,通过2种不同的方法将文本插入到我的UITextView中取决于按钮,这里的第一个方法是...

I have a few custom buttons that insert the text into my UITextView by 2 different methods depending on the button, the first method here is...

-(IBAction)string1:(id)sender {

NSRange range = myTextView.selectedRange;  
NSString * firstHalfString = [myTextView.text substringToIndex:range.location];  
NSString * secondHalfString = [myTextView.text substringFromIndex: range.location];  
myTextView.scrollEnabled = NO;  

NSString * insertingString = [NSString stringWithFormat:@"insert my text string"];

myTextView.text = [NSString stringWithFormat: @"%@%@%@",  
                 firstHalfString,  
                 insertingString,  
                 secondHalfString];  
range.location += [insertingString length];  
myTextView.selectedRange = range;  
myTextView.scrollEnabled = YES;}

,第二个方法是...



and the second method is ...

-(IBAction)string2:(id)sender {

myTextView.text = [myTextView.text stringByAppendingString:@"my other string value"];}

在NSLog打开的情况下, / p>

With NSLog turned on, I can see that

- (void)textViewDidChange:(UITextView *)textView{
 NSLog(@"something changed");}

确实被调用,但是这个代理从不被调用

does get called but this delegate never gets called

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
NSUInteger newLength = (textView.text.length - range.length) + text.length;
if(newLength <= MAX_LENGTH)
{
    return YES;
} else {
    NSUInteger emptySpace = MAX_LENGTH - (textView.text.length - range.length);
    textView.text = [[[textView.text substringToIndex:range.location] 
                      stringByAppendingString:[text substringToIndex:emptySpace]]
                     stringByAppendingString:[textView.text substringFromIndex:(range.location + range.length)]];
    return NO;
}
NSLog(@"something else changed");}

我的.M文件的顶部我已经定义 #define MAX_LENGTH 100

At the top of my .M file I have defined #define MAX_LENGTH 100

任何帮助将非常感谢,我的总体目标是限制UITextView中的章程数。

Any help would be very much appreciated, my overall goal is to limit the number of charters in the UITextView.

推荐答案

UITextView 只传送 textView:shouldChangeTextInRange :replacementText:到其代理以响应用户事件。当您的程序设置 myTextView.text 属性时,它不会发送该消息。

UITextView only sends textView:shouldChangeTextInRange:replacementText: to its delegate in response to user events. It does not send that message when your program sets the myTextView.text property.

UITextView 正在向代理发送 textViewDidChange:,因为文档说此方法是

I'm surprised that the UITextView is sending textViewDidChange: to the delegate, since the documentation says "This method is not called in response to programmatically initiated changes."

因此,您可以在 textViewDidChange:中剪辑文本并且依赖于它的行为与文档不同),或者修改 string1: string2:

So, you could clip the text in textViewDidChange: instead (and rely on its behavior being different from the documentation), or modify and string1: and string2: to clip the text.

这篇关于UITextView委托从来没有调用过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆