我有一个名为 myTextField 的TextField,我需要检查两件事:
为此,我试试这个:
if ([myTextField.text isEqualToString:@""]){
//My code
}
检查textField是否为空,但我如何检查第一个字母是否不是空格?
答案 0 :(得分:1)
要检查没有空格,并检查它是否为不为空,请尝试以下操作,请注意!
基本上是在说if((myTextField is not empty) && (myTextField does not have a prefix of " "))
if(![myTextField.text isEqualToString:@""] && ![myTextField.text hasPrefix:@" "]) {
// Do whatever you want
} else if(![myTextField.text isEqualToString:@""] && [myTextField.text hasPrefix:@" "]) {
// Do whatever we want to remove whitespace, you can use `stringByTrimmingCharactersInSet:` to remove white space
} else {
// Else do what you want if nothing else matches.
}