I am trying to get delegate methods to work with UITextView, but it's not working for some reason.
I have declared in my viewController.h that it is a UITextViewDelegate
I am trying to get the following code to work to erase the default code "TEXT" when I tap on the textView.
- (void)textViewDidBeginEditing:(UITextView *)textView {
if (myTextView.text == @"TEXT") {
[myTextView setText:@""];
}
NSLog(@"did begin editing");
}
I expected to the text to be cleared and to see the NSLog print when I tap on the textView and the keyboard appears. Nothing at all happens
Using a text view by the way because I need to scale the view based on its content size and seems that the textView has a contentSize property, whit label and textField do not.
UPDATE:
I should have used:
if ([myTextView.text isEqualToString:@"TEXT"]) {
[myTextView setText:@""]; }
here is the project if you want to take a look.
You use these methods to validate text that was typed by the user, to respond to specific interactions with the keyboard, and to control the overall editing process. Editing begins shortly before the text field becomes the first responder and displays the keyboard (or its assigned input view).
Overview. UITextView supports the display of text using custom style information and also supports text editing. You typically use a text view to display multiple lines of text, such as when displaying the body of a large text document. This class supports multiple text styles through use of the attributedText property ...
Delegation is a simple and powerful pattern in which one object in a program acts on behalf of, or in coordination with, another object. The delegating object keeps a reference to the other object—the delegate—and at the appropriate time sends a message to it.
The Delegate Pattern in Swift In Swift, a delegate is a controller object with a defined interface that can be used to control or modify the behavior of another object. One example is the UIApplicaitonDelegate in an iOS app.
this method is missing from your Test2ViewController.m file:
- (void)viewDidLoad {
[myTextView setDelegate:self];
}
or you can connect the delegate in the Interface Builder as well, if you prefer that way better.
UPDATE #1:
add this method to you class for controlling the return
key.
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if ([text isEqualToString:@"\n"]) {
NSLog(@"Return pressed, do whatever you like here");
return NO; // or true, whetever you's like
}
return YES;
}
Connect the delegate of the TextView in Interface Builder to the parent class. I like to use the connection in IB rather than code it. To me the less code to look at the better :). Also - don't compare strings that way. Use isEqualToString for string comparison:
if ([myTextView.text isEqualToString:@"TEXT"]) {
[myTextView setText:@""];
}
Here is the fixed project:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With