0

我想做的就是,如果没有任何内容UITextField,应用程序将显示警报。我不明白我在做什么错。这是我的代码:

- (IBAction)Calculate:(id)sender {
    /*If there is nothing on textfield, it will show an alert*/

    if ([addtake.text length] && [Subtake.text length] == 0) 
    {

            UIAlertView *myAlert = [[UIAlertView  alloc]initWithTitle:@"Alert"message:@"Empty   TextField not allowed" delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles: nil];    
            [myAlert show];
    }

    else{

            NSString *Adding =[addtake text];
            NSInteger number = [Adding intValue]; /*Converting String to int*/
            NSInteger new = 5 + number;
            NSString *newString = [NSString stringWithFormat:@"%d",new];
            [myAdd setText:newString];
            /*Subtruct*/
            NSString *Subtruct =[Subtake text];
            NSInteger SubNumber = [Subtruct intValue];
            NSInteger newSub = 10 - SubNumber;
            NSString *newSubString = [NSString stringWithFormat:@"%d",newSub];
            [mySub setText:newSubString];

            [addtake resignFirstResponder]; 
            [Subtake resignFirstResponder];        
      }
  }
4

3 回答 3

3

尝试使 if 语句中的每个子句都明确 - 如果只是为了可读性...

我会在你的测试中使用逻辑 OR,而不是逻辑 AND。因此,如果任一文本字段为空,则警报将显示

    if ((![addtake.text length]) || (![Subtake.text length])) 
    {

            UIAlertView *myAlert = [[UIAlertView  alloc]initWithTitle:@"Alert"message:@"Empty   TextField not allowed" delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles: nil];    
            [myAlert show];
    }
于 2012-05-18T07:58:04.673 回答
2
if ([addtake.text length] == 0 && [Subtake.text length] == 0) 
于 2012-05-18T07:46:09.097 回答
1

我假设 addText 是你的 tesxField 所以你需要设置条件 [addtake.text length] == 0 。

于 2012-05-18T07:45:53.867 回答