简体   繁体   English

如何验证文本字段仅显示2种文本

[英]How do I validate a textfield to only show 2 kinds of text

In iOS, whats the best way to validate a textfield to make sure that the text field has a certain text in it? 在iOS中,最好的方法是验证文本字段以确保文本字段中包含某个文本?

I have 我有

    TaxiOrCust.text

and I only want the words Customer or Taxi In the text field. 我只想在文本字段中输入CustomerTaxi字样。 the words must have the first letter in Uppercase. 单词必须有大写的第一个字母。

Is there a way to validate this in Xcode? 有没有办法在Xcode中验证这一点?

As mentioned, you REALLY shouldn't be using a textfield if the user is only allowed to enter two very specific things, but rather a picker or something similar. 如上所述,如果用户只被允许输入两个非常具体的东西,而不是一个选择器或类似的东西,那么你真的不应该使用文本字段。 BUT, if that is REALLY what you want to do for some other reason, the following code does just that: 但是,如果真的是你想要做的其他原因,下面的代码就是这样做的:

if ([TaxiOrCust.text isEqualToString:@"Customer"]) {
    NSLog(@"User entered 'Customer' with a capital C.");
}
else if ([TaxiOrCust.text isEqualToString:@"Taxi"]) {
    NSLog(@"User entered 'Taxi' with a capital T.");
}
else {
    NSLog(@"The user has entered an incorrect value.");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM