【问题标题】:Objective c checking whether text field is emptyObjective c 检查文本字段是否为空
【发布时间】:2010-07-04 03:58:15
【问题描述】:

代码如下:

- (IBAction) charlieInputText:(id)sender {
    //getting value from text field when entered
    charlieInputSelf = [sender stringValue];

    if (charlieInputSelf != @"") {
        //(send field if not empty
    }
}    

即使字段为空,也会发送它;因此,这并不像我想要的那样工作。

【问题讨论】:

    标签: objective-c xcode variables nstextfield


    【解决方案1】:

    仅检查 nil 以及文本长度是否大于 0 - 不为空

    if (textField.text && textField.text.length > 0)
    {
       /* not empty - do something */
    }
    else
    {
       /* what ever */
    }
    

    【讨论】:

    • 我觉得 Stack 已经成为比任何主要平台官方文档更有用的参考
    • 实际上如果 textField 为 nil 则 textField.text.lenght == 0。因此您可以将 if 减少为:if (textField.text.lenght > 0)
    • 这两个条件是相互依赖的。只保留一个条件。由于 0&0=0, 1&1=1
    【解决方案2】:

    我们已经有返回布尔值的内置方法,该值指示文本输入对象是否有任何文本。

    // In Obj-C
    if ([textField hasText]) {
            //*    Do Something you have text
        }else{
             /* what ever */
        }
    
    // In Swift
    
    if textField.hasText {
        //*    Do Something you have text
    }else{
         /* what ever */
    }
    

    【讨论】:

    • 我不知道 hasText !
    • 让 clientName = clientNameTF.hasText() ?客户端名称TF.text! : "客户名称"
    • 这是最优雅的答案!谢谢
    【解决方案3】:

    Joshua 在狭义的情况下有正确的答案,但一般来说,您不能使用 == 或 != 运算符比较字符串对象。您必须使用-isEqual: 或-isEqualToString: 这是因为charlieImputSelf 和@"" 实际上是指向对象的指针。虽然这两个字符序列可能相同,但它们不必指向内存中的相同位置。

    【讨论】:

    • 所以如果你想检查文本字段的值是否等于Boo,你必须使用if ([textField.text isEqualToString:@"Boo"])而不是if (textField.text == @"Boo")
    • 是的(我不能在这个文本框中输入“是”,这很烦人)
    【解决方案4】:

    那些“工作”在某种程度上。但是,我发现用户可以只用空格填写框。我发现使用正则表达式会有所帮助(尽管我使用的是没有空格的单词)我真的不知道如何允许空格。

    NSError *error = NULL;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[ ]" options:NSRegularExpressionCaseInsensitive error:&error];
    
    if (!([[inputField stringValue]isEqualTo:regex])) {
            NSLog(@"Found a match");
    // Do stuff in here //
    }
    

    【讨论】:

      【解决方案5】:

      最有效的方法是使用这个

      // set it into an NSString
      NSString *yourText = yourVariable.text;
      
      if([theText length] == 0])
      {
       // Your Code if it is equal to zero
      }
      else
      {
      // of the field is not empty
      
      }
      

      【讨论】:

        【解决方案6】:

        在 Swift 中检查文本字段是否为空

          @IBOutlet weak var textField: NSTextField!
          @IBOutlet weak var multiLineTextField: NSTextField!
        
          @IBAction func textChanged(sender: AnyObject) {
            //println("text changed! \(textField.stringValue)")
        
            if textField.stringValue.isEmpty == false {
              multiLineTextField.becomeFirstResponder()
              multiLineTextField.editable = true
            } else {
              multiLineTextField.editable = false
            }
          }
        

        【讨论】:

          【解决方案7】:

          最简单的方法。

          创建不带“”(空格)的新 NSString

          NSString *textWithoutSpaces = [self.textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
          

          现在你有了没有空格的字符串。 你只需要检查这个字符串是否为空。

          if (textWithoutSpaces != 0) {
           /* not empty - do something */
          } else {
            /* empty - do something */
          } 
          

          【讨论】:

            【解决方案8】:
            -(void)insert{
            
                if ([_nameofView  isEqual: @""]) {
                    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Alert"
                                                                                   message:@"Fill the Name Field First."
                                                                            preferredStyle:UIAlertControllerStyleAlert];
            
                    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                                                          handler:^(UIAlertAction * action) {}];
            
                    [alert addAction:defaultAction];
                    [self presentViewController:alert animated:YES completion:nil];
            
            
            
                }
                else if ([_detailofview  isEqual: @""]){
            
                    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Alert"
                                                                                   message:@"Fill the Details Field First."
                                                                            preferredStyle:UIAlertControllerStyleAlert];
            
                    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                                                          handler:^(UIAlertAction * action) {}];
            
                    [alert addAction:defaultAction];
                    [self presentViewController:alert animated:YES completion:nil];
            
                }
                else{
                    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Alert"
                                                                                   message:@"Data Inserted Successfully."
                                                                            preferredStyle:UIAlertControllerStyleAlert];
            
                    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                                                          handler:^(UIAlertAction * action) {}];
            
                    [alert addAction:defaultAction];
                    [self presentViewController:alert animated:YES completion:nil];
            
                }
            }
            

            【讨论】:

            • 我建议您在答案中添加一些解释性文字,否则您可能会因为“低质量”而将其删除(即使它可能是正确答案)!
            猜你喜欢
            • 2017-03-05
            • 2016-11-04
            • 2021-07-01
            • 2015-12-28
            • 2014-02-01
            • 1970-01-01
            • 1970-01-01
            • 2014-07-28
            • 1970-01-01
            相关资源
            最近更新 更多