0

我是 iphone 开发的新手。如果textfield's文本是否更改,我想发出警报消息。我如何检查文本字段的文本先前值和当前值?

我的代码是

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

4 回答 4

1

您需要创建一个属性:

@property (nonatomic, strong) NSString *lastTextInTextField;

像这样初始化它viewDidLoad

self.lastTextInTextField = txtCon.text;

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

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;
于 2013-05-30T06:32:10.093 回答
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];
       }

}
于 2013-05-30T05:51:45.487 回答
0
-(void)textFieldDidEndEditing:(UITextField *)textField
 {    

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

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

于 2013-05-30T05:56:26.507 回答
0

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

在 .h 文件中

@interface ViewController : UIViewController
{
    NSString * oldValue;
    NSString * newValue;
}

在 .m 文件中

- (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])中。因为如果您有多个文本字段,则最好使用原始名称。

于 2013-05-30T05:52:39.147 回答