0

I have placeholder text in a textfield (called countTotalFieldUniversal) that I want to change color when I click the Edit button.

Currently, my code is:

- (void)setEditing:(BOOL)flag animated:(BOOL)animated
{
  NSLog(@"setEditing called");
  [super setEditing:flag animated:animated];
  if (flag == YES){
    NSLog(@"Flag is yes");
    [countTotalFieldUniversal setValue:[UIColor darkGrayColor]
                            forKeyPath:@"_placeholderLabel.textColor"];
    NSLog(@"Color should be grey...");
}
else {
    // Edit not selected
  }
}

My console prints out that "Flag is yes" and "Color should be grey..." but the text doesn't change. Am I setting the text incorrectly?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
AllieCat
  • 3,890
  • 10
  • 31
  • 45
  • You may need to tell the field to redraw in order to pick up your key/value change: `[self.countTotalFieldUniversal setNeedsDisplay]`. – Kyle Truscott Jul 29 '13 at 01:55
  • I added that in right before NSLog(@"Color should be grey...");, but it didn't change. However, I had to do [countTotalFieldUniversal setNeedsDisplay]. It wouldn't accept self.countTotalFieldUniversal... – AllieCat Jul 29 '13 at 01:59
  • I agree with you that I need to "refresh" to table view. I'm just not sure how... – AllieCat Jul 29 '13 at 02:04
  • [tableview reloadData]? – lakshmen Jul 29 '13 at 02:12
  • Is your placeholder programmatically created, or done in the IB? Where are you calling the `-(void)setEditing:(BOOL)flag animated:(BOOL)animated`? – CaptJak Jul 29 '13 at 03:00
  • Is `countTotalFieldUniversal` a property on your class? You're referencing it like a local variable or an instance variable. Perhaps you're manipulating the wrong item? – Kyle Truscott Jul 29 '13 at 12:01

1 Answers1

0

Lakesh was mostly right, I needed to reloadData.

I changed the text color by adding a boolean (editClicked) to my setEditing function:

- (void)setEditing:(BOOL)flag animated:(BOOL)animated
{
  NSLog(@"setEditing called");
  [super setEditing:flag animated:animated];
  if (flag == YES){
    editClicked = true;
    [tableView reloadData]; 
}
else {
    editClicked = false;
  }
}

In my table's cellForRowAtIndexPath function, I then added the following if statement:

//If editClicked is true, change text to grey
                if (editClicked == true){
                    [countTotalFieldUniversal setValue:[UIColor darkGrayColor]
                                            forKeyPath:@"_placeholderLabel.textColor"];
                }
                else {
                    // Save the changes if needed and change the views to noneditable.
                    [countTotalFieldUniversal setValue:[UIColor blackColor]
                                            forKeyPath:@"_placeholderLabel.textColor"];
                }

Thanks everyone (esp Lakesh and Kyle) for the help!

AllieCat
  • 3,890
  • 10
  • 31
  • 45