Search code examples
iossetvaluesetediting

Turn text grey on "Edit button" click


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?


Solution

  • 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!