计算textview中的字符数

Leo*_*eon 0 character objective-c nstimer ios4

我想计算用户输入的textview中的字符数.我给了它一些想法,我的想法是我必须创建一个带选择器的NSTimer来检查长度.所以我有:

-(void)viewDidLoad { [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(checkText) userInfo:nil repeats:YES];  }

-(void)checkText {

int characters;

label.text.length = self.characters;

characterLabel.text = [NSString stringWithFormat:@"%i", characters]; }
Run Code Online (Sandbox Code Playgroud)

这不起作用,因为"请求成员"字符"在某种结构或联合的东西"

我怎么解决这个问题?

Bar*_*ski 5

没有必要使用NSTimer.将自己设置为textview的委托并实现:

- (void)textViewDidChange:(UITextView *)textView
{
    NSUInteger length;
    length = [textView.text length];

    characterLabel.text = [NSString stringWithFormat:@"%u", length]
}
Run Code Online (Sandbox Code Playgroud)