0

我创建了一个标签式温度转换器。一个选项卡提供转换器,另一个选项卡是UIWebView使用谷歌查找当地天气温度。现在我想通过在第一个选项卡中制作我的背景来使它变得更好,该选项卡是根据用户在UITextField. 例如,如果输入的字符串小于 0,我想要一张我将导入的雪景图片。所以我使用以下 if 和 else 语句。Xcode 没有给我错误,但代码不起作用。有人可以帮我完成我的项目吗?提前致谢!这是我的 if 和 else 代码:

-(UITextField *)startingTemperatureTextField {
if (startingTemperatureTextField.text < 0)
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Snowy Background.png"]];
}
4

2 回答 2

0

try like this..

-(UITextField *)startingTemperatureTextField {
     if (![startingTemperatureTextField.text length] > 0 ){
          [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"Snowy Background.png"]]];
     }
}

EDIT

I've completely misunderstood you question! Sorry!

So... you want to change the background if the user inputs a value into your "startingTemperatureTextField" that is less than 0, so i guess that somewhere in your code you have used this method :

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}

just change this method like this :

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
       if( textField == self.startingTemperatureTextField ){

            if ([self.startingTemperatureTextField.text intValue] < 0 ){
                 [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"Snowy Background.png"]]];
        }
    }
    textField resignFirstResponder];
    return YES;
}
于 2013-02-22T02:36:36.080 回答
0

您正在将字符串与 int 进行比较。使用 intValue 获取字符串的 int 值。

    -(UITextField *)startingTemperatureTextField {
        if ([startingTemperatureTextField.text intValue] < 0){
            self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage       imageNamed:@"Snowy Background.png"]];
        }
    }
于 2013-02-21T22:00:53.590 回答