我有两个UITextField。我想检查UITextField,如果两个UITextField是一些文本显示警报,或者只有一个UITextField有一些文本然后继续。
答案 0 :(得分:1)
您可以使用UITextFieldDelegate的两个委托功能
-(BOOL) textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
if ([self.textField1.text isEqualToString:self.textField2.text])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert"
message:@"text field have same text"
delegate:nil
cancelButtonTitle:@"Cancel"
otherButtonTitles:nil];
[alert show];
}
}
答案 1 :(得分:0)
像这样实施UITextField
的委托方法......
- (void)textFieldDidEndEditing:(UITextField *)textField
{
if ([textField1.text length] > 0 && [textField2.text length] > 0)
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:@"Hello" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
else
{
// go forward here.....
}
}
答案 2 :(得分:0)
if([txt1.text length] > 0 && [txt2.text length]>0)
{
UIAlertView * alert=[[UIAlertView alloc]initWithTitle:@"Alert" message:@"TextFied Have an Text" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil];
[alert show];
}
else
{
//Go Forword
}
试试此代码
答案 3 :(得分:0)
如果textField都有相同的文本,则显示警告,例如确认密码。
if ([firstTextField.text isEqualToString:secodTextField.text])
{
//Show Alert
return;
}
答案 4 :(得分:0)
以下是您的要求的完整代码。
// check if any text field has some text.
if ([txt1.text length] > 0 && [txt1.text length] > 0 )
{
// show alert
UIAlertView * alert=[[UIAlertView alloc]initWithTitle:@"Alert" message:@"TextFied Have an Text" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil];
[alert show];
}
// check if any one text field has some text
else if(([txt1.text length] > 0 && [txt2.text isEqualToString:@"") || ([txt2.text length] > 0 && [txt1.text isEqualToString:@"") )
{
// go forward
}
多数民众赞成。
答案 5 :(得分:0)
如果您想在textfield中省略空格,可以使用以下代码
if([[self.testfield1.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] > 0 && [[self.testfield2.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length]>0)
{
UIAlertView * alert=[[UIAlertView alloc]initWithTitle:@"Alert" message:@"TextFields Have an Text" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil];
[alert show];
}
else
{
UIAlertView * alert=[[UIAlertView alloc]initWithTitle:@"Alert" message:@"TextField not having an Text" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil];
[alert show];
}