Objective c checking whether text field is empty

后端 未结 8 1146
孤独总比滥情好
孤独总比滥情好 2021-02-05 01:11

Here\'s the code:

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

           


        
相关标签:
8条回答
  • 2021-02-05 01:17

    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.

    0 讨论(0)
  • 2021-02-05 01:19

    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 */
    }
    
    0 讨论(0)
  • 2021-02-05 01:21

    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 */
    }
    
    0 讨论(0)
  • 2021-02-05 01:30

    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
    
    }
    
    0 讨论(0)
  • 2021-02-05 01:34

    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
        }
      }
    
    0 讨论(0)
  • 2021-02-05 01:38
    -(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];
    
        }
    }
    
    0 讨论(0)
提交回复
热议问题