[iPhone SDK] [Beginner] UITextView textViewDidChange will not be called?

I have a small iPhone project with a UITextView in my view developed in Interface Builder. My Viewcontroller has an IBAction method, and I connected a UITextView to this IBAction. I also added .h to my controller <UITextViewDelegate>.

In my .m file, I added a method:

- (void)textViewDidChange:(UITextView *)textView{
     int count = [textView.text length];
     charCount.text = (NSString *)count;
}

But when the application is running, and I enter something into the textView, the textViewDidChange method will never be reached. Why is this? I also tried adding textView.delegate = self to the ViewDidLoad method, but then the application crashes without any message in the debugger.

Can someone tell me what I'm doing wrong?

Thank you very much

twickl

+3
2

, , !

charCount musst :

charCount.text = [[NSNumber numberWithInt:count] stringValue];

!

+1

- , , , , . , , testView.delegate = self; - textView? .

, textFieldDidChange: UITextFieldDelegate. , textField:shouldChangeCharactersInRange:replacementString: - , , . IBAction , , .

, , IB, . :

// MyViewController.h

@interface MyViewController : UIViewController  {
    UITextField *textView;
}

@property(nonatomic,retain) IBOutlet UITextField *textView;

- (IBAction)myAction:(id)sender;

@end

:

// MyViewController.m

@implementation MyViewController

@synthesize textView;

- (IBAction)myAction:(id)sender {
    // Do something
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
     int count = [textView.text length];
     charCount.text = (NSString *)count;
}

@end

UITextFieldDelegate.

+9

Source: https://habr.com/ru/post/1716239/


All Articles