20

How will I use the method addTarget with a UITextView? I know that you can use it with a UITextField, but I can't use it with a UITextView. I can't even create an action with a UITextView. Is there any way to do this? Thanks in advance.

  • `UITextView` is not meant to respond to events like that. What functionality are you after? – Dan Fairaizl Jul 07 '13 at 02:19
  • I just want to make a `NSString` variable that always has the same text as the `UITextView`'s, so I will need either `addTarget` or `IBAction` for that, right? And, by the way, the reason I put a `UITextView` instead of a `UITextField` is because I wanted this editable field to be multiline. –  Jul 07 '13 at 02:32
  • Why not just grab the text from the UITextView when needed? `myTextView.text`. If you need events when the user is interacting with the text view, take a look at the delegate. http://developer.apple.com/library/ios/#documentation/uikit/reference/UITextViewDelegate_Protocol/Reference/UITextViewDelegate.html – Dan Fairaizl Jul 07 '13 at 02:42
  • Hmm... It still doesn't work. I mean, it doesn't create an error (like before) or anything like that, but it doesn't do what I want it to do. I used the `textViewDidChange` method. Do you think that conforms to my example? –  Jul 07 '13 at 02:54
  • What events are you trying to use? Do you want to update your local storage variable on key press? Or when a user is done editing? – Dan Fairaizl Jul 07 '13 at 02:55
  • I want to update the variable on key press. –  Jul 07 '13 at 03:02
  • Check the answer below, hopefully that clears it up. I think all you were missing was the delegate :) – Dan Fairaizl Jul 07 '13 at 03:15

3 Answers3

15

Based on the information in the comments, what you'd like to do is update a local variable when the user types in a UITextView.

So try something like this (I assume you have a UIViewController subclass, and it's view has the UITextView in questions as a subview):

In your view controller, create an IBOutlet to your UITextView if using IB, or just a regular reference if not. Then another property for the NSString variable you want to store the text into.

NOTE: Make sure this view controller conforms to the UITextViewDelegate protocol as shown below.

@interface BBViewController () <UITextViewDelegate> //Note the protocol here

@property (weak, nonatomic) IBOutlet UITextView *textView;
@property (strong, nonatomic) NSString *userInput;

@end

Then, hook up the text view's delegate: (Or do this in IB)

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.textView.delegate = self;
}

Then when the user interacts with the text in that text view, it will send the proper delegate methods and you can update your variable appropriately.

#pragma mark - UITextViewDelegate

- (void)textViewDidChange:(UITextView *)textView {

    self.userInput = textView.text;
    NSLog(@"userInput %@", self.userInput); //Just an example to show the variable updating
}
Dan Fairaizl
  • 2,172
  • 2
  • 28
  • 31
  • Oh man!!!! The reason why I thought it didn't work was because I didn't put the `self.textView.delegate = self`! –  Jul 07 '13 at 03:43
  • Glad you figured it out, it's always the easy stuff :) – Dan Fairaizl Jul 07 '13 at 05:59
  • That's always the case, isn't it? –  Jul 08 '13 at 17:46
  • Oh also, I want to do the same thing but with a UIDatePicker. In other words, what is the "UIControlEventEditingChanged for a UIDatePicker"? –  Jul 09 '13 at 19:32
9

for swift version 3+

  1. add UITextViewDelegate to your class
  2. add delegate to your textview like this :
    self.mytextview.delegate = self
  3. add this method :

    func textViewDidChange(_ textView: UITextView){ 
        print("entered text:\(textView.text)")    
    }
    
Dani
  • 3,427
  • 3
  • 28
  • 54
iman kazemayni
  • 1,255
  • 1
  • 19
  • 20
1

You can achieve what you want using notifications.

//Listen to notifications :

NotificationCenter.default.addObserver(textView,
            selector: #selector(textDidChange),
            name: NSNotification.Name.UITextViewTextDidChange,
            object: nil)

//the function called when changed

    @objc private func textDidChange() {
        ...
    }

//make sure to release to observer as well
            NotificationCenter.default.removeObserver(textView,
            name: NSNotification.Name.UITextViewTextDidChange,
            object: nil)
HixField
  • 3,538
  • 1
  • 28
  • 54