0

我有两个 UITextField。我想检查 UITextField,如果两个 UITextField 是一些文本显示警报,或者如果只有一个 UITextField 有一些文本,那么继续。

4

6 回答 6

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];
}
}
于 2013-08-22T05:58:03.650 回答
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
   }

试试这个代码

于 2013-08-22T05:47:21.463 回答
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.....
   }
}
于 2013-08-22T05:40:45.347 回答
0

如果两个 textField 具有相同的文本,则显示警报,例如确认密码。

if ([firstTextField.text isEqualToString:secodTextField.text])
    {
        //Show Alert
        return;
    }
于 2013-08-22T05:51:16.927 回答
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  
}

而已。

于 2013-08-22T06:16:22.513 回答
0

如果您想省略文本字段中的空格,可以使用以下代码

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];

    }
于 2013-08-22T06:31:17.740 回答