-2

I have a if/else statement in my objective-c code. The if/else statement runs like this:

-(void)textViewDidBeginEditing:(UITextView *)textView
{
    if(textView==self.heardTextView)
    {
        NSString *string = textView.text;
        if ([string rangeOfString:@"CLOSER"].location == NSNotFound)    
        {
           NSLog(@"closest");
        } 
    }
}

The premise of the if/else statement is - if the textview equals a certain word some code will run. But this code isn't running.

I have put a breakpoint on my code and a NSLOG and nothing.

iGatiTech
  • 2,306
  • 1
  • 21
  • 45
fafafa
  • 57
  • 4

3 Answers3

0

check your text field delegate if you not set your delegate then it's not called..

In your Viewcontroller.H

enter image description here

select your textview give delegate on textview.

enter image description here

Maulik shah
  • 1,664
  • 1
  • 19
  • 45
0

First you need to set the delegate of the text view. You can do this with Interface Builder, but if you prefer using code you can do this in the viewDidLoad method of your view controller:

myTextView.delegate = self;

Now, when do you want the NSLog statement to be executed? Right now it looks like it will be run only if you type "CLOSER", then make the text view lose focus and then click on it again.

If you want the NSLog to run as you type, you should use:

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text

And also be careful, you need to calculate the next string value in the text field before testing its contents:

NSString *finalText = [textField.text stringByReplacingCharactersInRange:range withString:string];
Diego
  • 577
  • 4
  • 14
0

Check your delegates, you must not have made the delegate of your textview as self. Check it once again. Or you might have done to some other class.