Iphone 如何检查文本字段';s上一个值和修改后的值
我是iphone开发新手。如果Iphone 如何检查文本字段';s上一个值和修改后的值,iphone,uitextfield,Iphone,Uitextfield,我是iphone开发新手。如果textfield的文本是否更改,我想发出一条警告消息。我如何检查textfield的文本先前值和当前值 我的代码是 UITextField *txtCon; NSString *strname; txtCon.text = @"hello"; strname = txtCon.text; if(txtCon.text == strname) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:
textfield的
文本是否更改,我想发出一条警告消息。我如何检查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];
}
试试这个
-(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];
}
}
如果(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;
}
}
否则您的代码看起来不错。textfielddidediting
在此时textfield编辑结束时调用,如果它不等于字符串的新值,请将其与以前的值进行比较
如果([txtCon.text IsequalString:strname]),我建议您在此代码处使用原始的textfield名称,而不是textfield
。因为如果您有多个文本字段,最好使用原始名称
-(void)textFieldDidEndEditing:(UITextField *)textField
{
if ([textField isEqual:txtCon])
{
// Code to get edited value of textfield
}
}
如果有很多文本字段,这将很有帮助 您需要创建一个属性:
@property (nonatomic, strong) NSString *lastTextInTextField;
在viewDidLoad
中初始化它,如下所示:
self.lastTextInTextField = txtCon.text;
然后,当按下按钮时,将保留的textfield值与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;
thanx寻求帮助..但我想检查按钮的单击事件上的值是否已更改..textfield的委托上没有。而不是-(void)textfieldDendediting:(UITextField*)textfield
编写按钮单击事件。是的,这很好。但是如果我更改textfield中的任何文本,该文本将存储在straname中..而这不会存储在其他部分中。我想存储textfield previose值和thn modifield值..,然后比较以前的值和当前值。因此,现在,当您第一次单击按钮时,oldVlue为nil,新值必须为某物,因此,进入其他部分,newvlue将存储在oldvalue中。现在,如果您第二次单击按钮而不更新文本字段值,则oldvlue和newvalue将是相同的,并且if循环将执行。@Dilip,您不需要两个值来执行此操作,只需使用一个属性即可,可以检查我的答案吗