计算文本视图中的字符数
我想计算文本视图中用户输入的字符数。我给了它一些想法,我的想法是我必须创建一个带有检查长度的选择器的 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]; }
这不起作用,因为“请求非结构或联合中的成员“字符””
我该如何解决这个问题?
i want to count the characters in a textview which are typed in by the user. I gave it some thoughts and my ideas were that I have to create a NSTimer with a selector which checks the length.
So I've got:
-(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]; }
This doesn't work because "Request for member "characters" in something not a structure or union"
How can I solve this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不需要 NSTimer。将自己设置为 textview 的委托并实施:
There is no need for NSTimer. Setup yourself as textview's delegate and implement:
请注意,如果您尝试使用 self.characters 来获取 int 字符 变量的值 - 它不会工作。
self.characters
代码意味着您有一个属性设置@propertycharacters
并且它将调用该属性的 getter。要使用局部变量只需使用它的名称。我认为您的
-checkText:
方法中存在混合的标签对象。以下是如何获取 UITextView 的字符数的通用示例:
Please note that if you are trying to use
self.characters
to getint characters
variable's value - it won't work.self.characters
code means you have a property setup@property characters
and it will call this property's getter. To use local variable just use its name.I think you have mixed-up label objects in your
-checkText:
method.Here's a generic example of how to get
UITextView
's number of characters: