38

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.

objectiveccoder001
  • 2,981
  • 10
  • 48
  • 72

8 Answers8

88

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 */
}
Joshua Weinberg
  • 28,598
  • 2
  • 97
  • 90
57

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 */
}
Gaurav Pandey
  • 1,953
  • 3
  • 23
  • 37
8

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.

JeremyP
  • 84,577
  • 15
  • 123
  • 161
  • 3
    So if you would want to check if the value of the text field was equal to `Boo`, you'd have to use `if ([textField.text isEqualToString:@"Boo"])` instead of `if (textField.text == @"Boo")` – Douwe Maan Jul 04 '10 at 12:52
  • 1
    Yes (it's annoying I can't just enter 'yes' in this text box) – JeremyP Jul 04 '10 at 15:50
2

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 //
}
Jon Wei
  • 1,481
  • 2
  • 10
  • 18
1

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

}
1

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
    }
  }
seinfeld
  • 1,686
  • 1
  • 17
  • 18
1

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

    }
}
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
KR MIrza
  • 1
  • 1
  • I would recommend that you add some explanatory text to your answer, or you risk having it deleted as 'low quality' (even though it may be a correct answer)! – Adrian Mole Oct 12 '19 at 06:37