limit number of characters user may enter into multiple UITextViews

丶灬走出姿态 提交于 2019-12-14 03:59:36

问题


In some sense this question has already been answered at Limit number of characters in uitextview. But my particular case is that I have more than one textview in the same ViewController. So I am not sure how to fix that problem. Say I only have two textViews. How might I handle these cases:

  • they both have the same character limit?

  • each has different character limit? say 300 and 400 respectively.

Do I use IBAction? If yes how?


回答1:


So you need IBOutlet for both textviews

@property (weak, nonatomic) IBOutlet UITextField *textfield1;
@property (weak, nonatomic) IBOutlet UITextField *textfield2;

then in your delegate method

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text

You simply add a check for the right textfield

if (self.textfield1 == textfield) {
// handle first text field here

} else {
// handle second text field here
}


来源:https://stackoverflow.com/questions/25272413/limit-number-of-characters-user-may-enter-into-multiple-uitextviews

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!