图像更改验证

时间:2014-03-21 11:33:38

标签: objective-c ios7 xcode5

我想在验证中更改图像,就像文本字段为空,图像颜色为灰色或填充圆圈为绿色。

我在按钮操作中尝试了这个 -

UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 50)];
imgView.image = [UIImage imageNamed:@"a_image.png"];
imgView.contentMode = UIViewContentModeCenter;

3 个答案:

答案 0 :(得分:0)

应该很简单:

if (textField.text.length == 0){
   //Do something if its empty
} else {
   //Do something else if its not
}

编辑:如果您想要动态执行此操作,而不是在他们完成编辑后,您可以执行以下操作:

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.textField addTarget:self action:@selector(textChanged) forControlEvents:UIControlEventEditingChanged];
}

- (void)textChanged
{
    if (self.textField.text.length == 0) {
        self.someVIew.backgroundColor = [UIColor grayColor];
    } else if (self.textField.text.length > 5){
        self.someVIew.backgroundColor = [UIColor redColor];
    } else {
        self.someVIew.backgroundColor = [UIColor greenColor];
    }
}

答案 1 :(得分:0)

UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 50)];

if (textField.text.length == 0){
   imgView.image = [UIImage imageNamed:@"firstImage.png"];
} else {
   imgView.image = [UIImage imageNamed:@"secondImage.png"];
}

答案 2 :(得分:0)

在按钮操作中尝试此操作。

  if (self.MyTextField.text.length!=0)
        self.MyImageView.image=[UIImage imageNamed:@"redImage"];
    else
        self.MyImageView.image=[UIImage imageNamed:@"greenImage"];

如果您想在用户输入文本字段时更改颜色,请使用textfield的以下委托方法来执行任何操作。

  

- (void)textFieldDidBeginEditing:(UITextField *)textField;