否则如果改变流量

时间:2014-08-18 19:45:07

标签: objective-c if-statement nsstring

我有一系列其他if语句。基本上我检查用户输入的数据是否有效。我有一个文本字段,用户可以在其中输入交易的标题。我检查以确保他们输入了一些东西。然后我想确保他们输入的不仅仅是空白区域。

正如您在我的代码中看到的,当用户输入无效时,我输入else if并退出方法,确保未添加交易。

但是我有问题。要查看用户是否输入了空格,我必须首先解决此问题,但这是在else if中。

有效地我的其他地方如果应该是'如果有空白的话,那么输入其他如果并返回'。但这是不可能的,因为我不知道是否有空白区域。

任何人都可以帮助解决流量问题吗?

我的其他如果有空格,表达式必须基本为真。

非常感谢您的帮助。

//checks if something has been entered. That something could be a space. If nothing is entered length is 0 and message is displayed
if (![self.dealTitleTextField.text length]> 0) {
    NSLog(@"Deal Title is empty");
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Not quite!" message:@"You haven't entered a title and you have to have one. Please do so." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
    [alert show];
    return ;
}//start of new else you entered 18th august and needs to be reworked
else if ([self.dealTitleTextField.text length] > 0){
    NSCharacterSet *charSet = [NSCharacterSet whitespaceCharacterSet];
    NSString *trimmedString = [self.dealTitleTextField.text stringByTrimmingCharactersInSet:charSet];
    if ([trimmedString isEqualToString:@""]) {
        // String contains only whitespace.
        NSLog(@"Deal Title is made up just of white space");
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Not quite!" message:@"You haven't entered a title that is made up purely of white space. This isn't allowed." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alert show];
        return ;
    }
    NSLog(@"Deal Title - End of check");
}
//end of new else you entered 18th august that needs to be reworked
else if (self.dealModel.dealTypeModel == NULL){
    NSLog(@"Deal Type is empty");
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Not quite!" message:@"You haven't entered a deal type and you have to have one. Please do so." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
    [alert show];
    return;
}else if (![self.dealDescriptionTextArea.text length]> 0){
    NSLog(@"Deal TextArea is empty");//more code below not included as out of scope

3 个答案:

答案 0 :(得分:0)

首先修剪空白区域。

然后你只需要检查长度是不是0.

不要区分白色空间和任何东西。我认为让用户复杂化。如果他们只输入白色空格然后将其清除为空,那么他们可以再次输入。

此外,您的第三个和第四个else if块应该是单独的if语句。

三组ifs。

Trim white space from title.
If deal title is length 0 then fail.
If deal type is null then fail.
If deal description is blank then fail.

他们不仅仅是ifs。

答案 1 :(得分:0)

我认为应该改为:

NSString *trimmedString = [self.dealTitleTextField stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]];

if (trimmedString.length)
{

}
else
{

}

请注意,不需要包含> 0,因为0将评估为NO,任何大于0的评估结果为YES。

希望有所帮助。

答案 2 :(得分:-1)

您可以使用此功能检查文本是空或空白还是只有空格:

- (BOOL) isNullString : (NSString *)textString{
     if (textString == nil || textString == (id)[NSNull null] || [[NSString stringWithFormat:@"%@",textString] length] == 0 || [[[NSString stringWithFormat:@"%@",textString] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0) {
        return YES;
     }
     else{
        return NO;
     }
}