Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextView delegate methods

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.

like image 530
Mr Ordinary Avatar asked Aug 06 '12 08:08

Mr Ordinary


People also ask

What is the use of delegate keyboard?

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).

What is UITextView?

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 ...

What is delegate in Uikit?

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.

What is a delegate in Swift?

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.


2 Answers

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;
}
like image 138
holex Avatar answered Oct 12 '22 14:10

holex


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:@""];
}

Connection Pic

Here is the fixed project:

like image 38
LJ Wilson Avatar answered Oct 12 '22 15:10

LJ Wilson