如何检查文本字段的先前值和修改后的值

时间:2013-05-30 05:42:41

标签: iphone uitextfield

我是iphone开发的新手。如果textfield's文本是否更改,我想发出警告消息。我可以查看textfield的文本以前的值和当前值吗?

我的代码是

UITextField *txtCon;

NSString *strname;

txtCon.text = @"hello";

strname = txtCon.text;

if(txtCon.text == strname)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Rate on the Appstore!" 
                                                message:@"changed" 
                                               delegate:self 
                                      cancelButtonTitle:@"Later" 
                                      otherButtonTitles:@"OK", nil];
[alert show];
[alert release];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Rate on the Appstore!" 
                                                message:@"not changed" 
                                               delegate:self 
                                      cancelButtonTitle:@"Later" 
                                      otherButtonTitles:@"OK", nil];
[alert show];
[alert release];
}

4 个答案:

答案 0 :(得分:1)

试试这个...

 -(void)textFieldDidEndEditing:(UITextField *)textField
    {
        if ([textField.text isEqualToString:strname])
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Rate on the Appstore!" 
                                                    message:@"changed" 
                                                   delegate:self 
                                          cancelButtonTitle:@"Later" 
                                          otherButtonTitles:@"OK", nil];
             [alert show];
             [alert release];
       }
       else
       {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Rate on the Appstore!" 
                                                    message:@"not changed" 
                                                   delegate:self 
                                          cancelButtonTitle:@"Later" 
                                          otherButtonTitles:@"OK", nil];
           [alert show];
          [alert release];
       }

}

答案 1 :(得分:1)

您需要创建一个属性:

@property (nonatomic, strong) NSString *lastTextInTextField;

viewDidLoad中将其初始化为:

self.lastTextInTextField = txtCon.text;

然后按下按钮时,将保留的文本字段值与textfields字符串进行比较:

if ([self.lastTextInTextField isEqualToString:txtCon.text])
{
    // matches the old one;
}
else
{
    // not matched
}

// then just set this property value to current textfield string.
self.lastTextInTextField = txtCon.text;

答案 2 :(得分:0)

if(txtCon.text == strname)这段代码错了你不能像这样比较两个字符串。 正确的方法是,

<。>文件中的

@interface ViewController : UIViewController
{
    NSString * oldValue;
    NSString * newValue;
}
<。>文件中的

- (void)viewDidLoad
{
    oldvalue = txtCon.text;
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{    
   newValue = txtCon.text
}

- (IBAction)yourBtn:(id)sender {    
   if ([oldvalue isEqualToString:newValue]) {
           //write your alert code here
    }else{
          //write your alert code here
          oldvalue =newValue;
    }
}

否则你的代码看起来不错。textFieldDidEndEditing当文本字段编辑结束时调用,如果它不等于为字符串设置新值,则将其与之前的值进行比较。

我建议您在此代码textField上使用原始文本字段名称而不是if ([txtCon.text isEqualToString:strname])。 becoz如果你有多个文本字段而不是使用原始名称。

答案 3 :(得分:0)

-(void)textFieldDidEndEditing:(UITextField *)textField
 {    

    if ([textField isEqual:txtCon])
    {
        // Code to get edited value of textfield
    }
}

如果有很多文本字段,这将有所帮助..