UITextField在焦点上或使用objective-c开始编辑输入字段的背景颜色变化

时间:2015-07-12 06:38:35

标签: ios objective-c iphone

我是目标-c的新手,我遇到问题,我有一个输入文件,我试图在用户在UITextField上开始输入时更改输入字段背景颜色 对于输入文件我的代码是下面的

SELECT GEOGRAPHY , PROJECT ,
  CASE
    WHEN project_name = 'test1'
    THEN 'test1'
    WHEN project_name = 'test2'
    THEN 'test2'
    WHEN project_name    = 'test3'
    AND test_NAME = 'samp1'
    THEN 'gen1'
    WHEN project_name     = 'test3'
    AND  TEST_name <> 'samp1'
    THEN 'gen2'
    ELSE project_name
  END PROJECT_NAME from table 1
group by 
geography ,project_name,test_name.

提前致谢

1 个答案:

答案 0 :(得分:2)

选项1,使用委托

  1. 将textfield Delegate设置为self
  2. 设置viewController符合UITextfieldDelegate

    -(void)textFieldDidBeginEditing:(UITextField *)textField{
        textField.backgroundColor = [UIColor blueColor];
    }
    -(void)textFieldDidEndEditing:(UITextField *)textField{
        textField.backgroundColor = [UIColor whiteColor];
    }
    -(BOOL)textFieldShouldReturn:(UITextField *)textField{
      [textField resignFirstResponder];
      return true;
    }
    
  3. 选项2,使用目标操作

        [textfield addTarget:self action:@selector(textFieldBeginEditing:) forControlEvents:UIControlEventEditingDidBegin];
    

    然后在函数中

    -(void)textFieldBeginEditing:(UITextField *)textfield{
    textfield.backgroundColor = [UIColor blueColor];
    }