2012-05-18 163 views
0

我想要做的,如果UITextField上沒有任何內容,應用程序將顯示警報。我不明白我在做什麼錯。這裏是我的代碼:如果UITextField爲空,則顯示UIAlert

- (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];   
     } 
    } 
+0

其中textField?吸收或吸收? – Shmidt

回答

3

嘗試使每個子子句中的if語句明確的 - 如果僅僅是爲了便於閱讀...

我會用邏輯或,而不是邏輯並在測試。因此,如果任一文本字段爲空,警報將顯示

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]; 
    } 
+0

是的,這是最好的解決方案:-) – Adnan

1

我認爲addText是你tesxField所以你需要設置的條件[addtake.text長度] == 0以及。

2
if ([addtake.text length] == 0 && [Subtake.text length] == 0) 
相關問題