我只想做,如果 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/