limit number of characters user may enter into multiple UITextViews
Asked Answered
C

1

1

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?

Chamkis answered 12/8, 2014 at 19:22 Comment(2)
It's basically the same answer. Just use the textView property to test which text view is being processed.Dimorphous
@Dimorphous I like when the answers to my BIG questions are so simple. thanks!Chamkis
J
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
}
Jinajingle answered 12/8, 2014 at 19:32 Comment(1)
If you didn't want to keep the textViews as properties for whatever reason, you could also use tags. textView1.tag = 1; textView2.tag = 2; if (textView.tag == 1) { // do something } else { // do something else }Thrombokinase

© 2022 - 2024 — McMap. All rights reserved.