【问题标题】:Turn text grey on "Edit button" click在“编辑按钮”上将文本变为灰色单击
【发布时间】:2013-07-29 01:50:30
【问题描述】:

我在一个文本字段(称为 countTotalFieldUniversal)中有占位符文本,我想在单击“编辑”按钮时更改颜色。

目前,我的代码是:

- (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
  }
}

我的控制台打印出“标志是”和“颜色应该是灰色...”,但文本没有改变。 我是否设置了错误的文本?

【问题讨论】:

  • 您可能需要告诉该字段重绘以获取您的键/值更改:[self.countTotalFieldUniversal setNeedsDisplay]
  • 我在 NSLog(@"Color should be gray..."); 之前添加了它,但它没有改变。但是,我必须做 [countTotalFieldUniversal setNeedsDisplay]。它不会接受 self.countTotalFieldUniversal...
  • 我同意你的观点,我需要“刷新”到表格视图。我只是不确定如何...
  • [tableview reloadData]?
  • 您的占位符是通过编程方式创建的,还是在 IB 中完成的?你在哪里打电话给-(void)setEditing:(BOOL)flag animated:(BOOL)animated

标签: ios setvalue setediting


【解决方案1】:

Lakesh 基本正确,我需要重新加载数据。

我通过在我的 setEditing 函数中添加一个布尔值 (editClicked) 来更改文本颜色:

- (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;
  }
}

在我的表格的 cellForRowAtIndexPath 函数中,我添加了以下 if 语句:

//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"];
                }

感谢大家(尤其是 Lakesh 和 Kyle)的帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-04
    • 1970-01-01
    • 2015-04-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多