iphone - 如果 UITextField 为空则显示 UIAlert

标签 iphone ios ios5

我只想做,如果 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];        
      }
  }

最佳答案

尝试使您的 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];
    }

关于iphone - 如果 UITextField 为空则显示 UIAlert,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10648634/

相关文章:

objective-c - 按 block 包装 c 回调(__bridge_transfer 和 block )

ios5 - 如何仅旋转屏幕层次结构中的一个屏幕

objective-c - 为什么这个 UIButton/IBAction 不刷新我的页面?

iphone - 这是 CoreText 吗? - 鸟舍文本捏缩放展开

iphone - 申请因未遵循 iOS 数据存储指南而被拒绝

ios - AFNetworking:具有子域的 AFHTTPClient

ios - 使用自定义 UIImage 更改 FBLoginView 大小看起来很糟糕

ios - XCode 4.5 分析工具

ios - Swift 中合并子数组

iphone - iPhone SDK-<3.2中的CATextLayer崩溃