How to know if a UITextField in iOS has blank spaces

前端 未结 9 794
一整个雨季
一整个雨季 2020-12-01 03:33

I have a UITextField where user can enter a name and save it. But, user should not be allowed to enter blank spaces in the textFiled.

1 - How can I find out,

9条回答
  •  渐次进展
    2020-12-01 04:08

    Here's what I did using stringByReplacingOccurrencesOfString.

    - (BOOL)validateFields
     {
          NSString *temp = [textField.text stringByReplacingOccurrencesOfString:@" "
                                                              withString:@""
                                                                 options:NSLiteralSearch
                                                                   range:NSMakeRange(0, textField.text.length)];
    
          if ([temp length] == 0) {
              // Alert view with message @"Please enter something."
              return NO;
          }
     }
    

提交回复
热议问题