Objective C 检查文本字段是否为空

发布于 2024-09-07 23:39:25 字数 296 浏览 6 评论 0原文

这是代码:

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

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

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

Here's the code:

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

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

This sends it even when the field is empty; therefore, this does not work as I want it to.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

流绪微梦 2024-09-14 23:39:25

只需检查 nil 以及文本长度是否大于 0 - 非空

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

Simply checks for nil and if length of text length is greater than 0 - not empty

if (textField.text && textField.text.length > 0)
{
   /* not empty - do something */
}
else
{
   /* what ever */
}
羁绊已千年 2024-09-14 23:39:25

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

// 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 */
}

We already have inbuilt method that return boolean value that indicates whether the text-entry objects has any text or not.

// 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 */
}
世俗缘 2024-09-14 23:39:25

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

Joshua has the right answer in the narrow case, but generally, you can't compare string objects using the == or != operators. You must use -isEqual: or -isEqualToString: This is because charlieImputSelf and @"" are actually pointers to objects. Although the two sequences of characters may be the same, they need not point at the same location in memory.

箹锭⒈辈孓 2024-09-14 23:39:25

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

NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[ ]" options:NSRegularExpressionCaseInsensitive error:&error];

if (!([[inputField stringValue]isEqualTo:regex])) {
        NSLog(@"Found a match");
// Do stuff in here //
}

Those 'work' in a way. However, I found that the user can just fill in the box with spaces. I found using regular expressions helps (though what I use is for words without spaces) I can't really figure out how to make allow spaces.

NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[ ]" options:NSRegularExpressionCaseInsensitive error:&error];

if (!([[inputField stringValue]isEqualTo:regex])) {
        NSLog(@"Found a match");
// Do stuff in here //
}
油饼 2024-09-14 23:39:25

最有效的方法是使用这个

// 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

}

The Most efficent way to do this is by using this

// 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

}
掐死时间 2024-09-14 23:39:25

检查 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
    }
  }

Check whether text field is empty in 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
    }
  }
萧瑟寒风 2024-09-14 23:39:25

最简单的方法。

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

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

现在您有了不带空格的字符串。
您只需检查该字符串是否为空。

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

The easiest way to do it.

Create new NSString without " " (spaces)

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

Now you have string without spaces.
You just have to check is this string empty or not.

if (textWithoutSpaces != 0) {
 /* not empty - do something */
} else {
  /* empty - do something */
} 
毅然前行 2024-09-14 23:39:25
-(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];

    }
}
-(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];

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文