Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Implementing Designs for iPhone Using Auto Layout and Managing the Keyboard Handling Views Covered by the Keyboard

Jeroen de Vrind
Jeroen de Vrind
29,772 Points

How to access specific textField from another class?

Since TPKeyboardAvoidingScrollView is my UITextField delegate, I have to implement the method:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {}

in that class.

I want to use this method to set the maximum number of characters that the user can put into this field. How can I reach a specific textfield like passwordField, which is in my ScrollView, from this class? Because the passwordField property is in the SignUpViewController or LogInViewController class. Do I have to import these classes in my TPKeyboardAvoidingScrollView class and in which way can i access them? Or can I have multiple delegates for my UITextView?

4 Answers

Jeroen de Vrind
Jeroen de Vrind
29,772 Points

Thx Gareth. I made LoginViewController the delegate and put the method in that class. The method won't be triggered. Maybe it's because the textfield is in a ScrollView with it's own class:TPKeyboardAvoidingScrollView which is in LoginViewController?

Gareth Borcherds
Gareth Borcherds
9,372 Points

Did you link up your text field to the class as a delegate? If you're using storyboards, you can drag and drop from the connections inspector and create the delegate connection.

Gareth Borcherds
Gareth Borcherds
9,372 Points

You can have as many delegates for your UITextView as you'd like. I'm pretty sure that delegates need to be placed in the view controller that controls the view. So, it sounds like you would need to create a delegate in SignUpViewController and LogInViewController for UITextView and implement those functions that you want for it there.

It's also important to note that if you have multiple UITextView's in the same view controlled by the same view controller, then you can implement the delegate for UITextView in that controller, but control the behavior of all your UITextViews. The method you posted above passes the textField as an argument, and you can use that to create if statements to manage multiple in the same controller.

Hope that helps.

Gareth Borcherds
Gareth Borcherds
9,372 Points

Awesome. Glad you got it to work. Make sure to mark best answer to show this one as solved.