0

I have a UIButton that is gray when the user enters a viewController. There is also a UITextView. If the user enters text into the UITextView, then the button should turn red, but if the text view is blank, the the button is gray. If was thinking of doing something like bellow, which does change the color to red if the user enters text, but if the user deletes the text, it stays red instead of going back to gray. Here is the code I am using:

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

if (self.textView.text.length == 0) {
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor colorWithRed:193/255.5 green:193/255.0 blue:193/255.0 alpha:1.0];
}else{

    self.navigationItem.rightBarButtonItem.tintColor = [UIColor redColor];
}

if (self.titleView.text.length == 0) {
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor colorWithRed:193/255.5 green:193/255.0 blue:193/255.0 alpha:1.0];
}else{

    self.navigationItem.rightBarButtonItem.tintColor = [UIColor redColor];
}

NSLog(@"Typing has stopped");

}
matthew
  • 467
  • 8
  • 23

3 Answers3

4

Use if (self.textView.text.length == 0) instead of checking if the text is null.

Janum Trivedi
  • 1,660
  • 2
  • 16
  • 24
  • This works, but you have to dismiss the keyboard for the button to change color. I would like the button to change color the second any type is entered – matthew Feb 02 '14 at 18:02
  • Then you're using the wrong `UITextView` delegate method. `-textViewDidEndEditing` is called when the responder (i.e., the keyboard) is resigned. Try using `-textViewDidChange` instead. – Janum Trivedi Feb 02 '14 at 18:05
  • Once again, works perfectly. One last question (sorry for asking multiple question) I now have a second text view and I would only like the button to change color if both textView contain text. Now when both textViews do have text, the button is still gray. I changed the code I am using above – matthew Feb 02 '14 at 18:11
  • Try something like `(if (self.textView.text.length != 0) && (self.titleView.text.length !=0))` – Janum Trivedi Feb 02 '14 at 18:43
  • Also, for future reference, don't change the code you post from your original question, because if you do, then the post doesn't make sense to other viewers. – Janum Trivedi Feb 02 '14 at 18:44
1

For Swift 3:

if (self.textView.text.characters.count == 0)

Taichi Kato
  • 602
  • 9
  • 24
-1

You could bind the button image to the text view value and then use a value transformer to put in different colored images depending on if there's input.

Code is for OSX but hopefully it's adaptable.

@implementation DBHasTextImageTransformer

- (id)init
{
  self = [super init];
  if (self) {
    // Initialization code here.
  }

  return self;
}


+ (Class)transformedValueClass
{
  return [NSImage class];
}


+ (BOOL)allowsReverseTransformation
{
  return NO;
}


- (id)transformedValue:(id)value
{

  if ((value == NULL) || [value isEqualToString:@""]) {

    return [NSImage imageNamed: @"NoTextImage"];

  } else {

    return [NSImage imageNamed: @"HasTextImage"];

  }

}


@end