0

I saw this answer of how to create a placeholder for UITextView.

I took the following steps:

  1. Add to the .h class the declaration:

    @interface AdjustPhotoViewController : UIViewController<UITextViewDelegate>
    
  2. Added the method:

    - (BOOL) textViewShouldBeginEditing:(UITextView *)textView
    {
        NSLog(@"%d",[textView tag]);
        if ([textView tag]==1){
            campaignTitle.text = @"";
        }else{
            campaignDescription.text = @"";
        }
    
        return YES;
    }
    

But I don't see that the method is being invoked!

What am I missing?

textView is already delegated via the storyboard to the view

SOLVED:

The problem was that it wasn't delegated. Although I was using storyboard - it was only an outlet, not a delegate. Remember that if you are using storyboard, you need to delegate also from the text view to the orange button of the view! not only the other way

Community
  • 1
  • 1
Dejell
  • 13,947
  • 40
  • 146
  • 229
  • Well yes you have to set its delegate for delegate methods to be called. You can link the delegate like you did on the IB, or you can use its property to set the delegate there (self.textViewName.delegate = self;) – Pochi Jan 09 '13 at 02:51

1 Answers1

0

What am I missing?

Actually setting the delegate.

textView.delegate = self;

Merely conforming to a protocol won't magically make your object into the delegate of an arbitrary object; that's just a formal thing, and anyways, how on Earth would the UITextField know which particular instance of the class it has to assign its delegate?

  • @Odelya of course immediately after the code in which you create the text view. –  Sep 06 '12 at 17:12
  • textView - does it have to be the actual name of the textView? Since I would like to make it general for all the text views on the screen, not for a specific one. I tried to add textView.self to the method textViewShouldBeginEditing but it didn't help – Dejell Sep 06 '12 at 17:16
  • @Odelya you should probably fire up a C and Objective-C tutorial if it's not obvious. That has to be the actual name, since it's an instance property access that happens. –  Sep 06 '12 at 17:19
  • It doesn't help! I changed it to be - (BOOL) textViewShouldBeginEditing:(UITextView *)campaignTitle where campaignTitle is already delegated with the storyboard - but it doesn't reach the method – Dejell Sep 06 '12 at 17:29
  • @Odelya are you sure the text view itself is non-nil at the moment when you set the delegate? –  Sep 06 '12 at 17:30
  • Sure. I just deleted the delegate in the storyboard and created it again. I have a campaignTitle of type UITextView that is delegated to the UIViewController – Dejell Sep 06 '12 at 17:34
  • @Odelya you're missing something else that cannot be deduced from the details you provided so far. –  Sep 06 '12 at 17:37
  • I gave you V although your answer didn't help me much since I was using storyboard. I edited my question with the correct answer but since you gave me a clue - it's something.. see this one http://stackoverflow.com/questions/9253957/uitextview-shouldchangetextinrange-delegate-not-called – Dejell Sep 06 '12 at 22:12
  • @Odelya thanks - I couldn't help more, unfortunately. Interface Builder and all its stuff is a big mysterious mess. I advice you to do as much of the GUI from code as possible. Also, you can answer your own question and accept it (although it won't give you the +15 rep). –  Sep 06 '12 at 22:15
  • Not sure, I actually think that it saves time and I am Java programmer that wrote JSF pages from scratch with 10 fingers.. One just need to get used to it – Dejell Sep 06 '12 at 22:17