2010-07-04 5 views
34

Ecco il codice:Objective C verificare se campo di testo è vuoto

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

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

Questo invia anche quando il campo è vuoto; quindi, questo non funziona come voglio.

risposta

80

controlla semplicemente per nulla e se la lunghezza della lunghezza del testo è maggiore di 0 - non vuota

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

mi sento come Stack è diventato un riferimento più utile qualsiasi documentazione ufficiale sulle piattaforme principali – ChuckKelly

+2

In realtà se textField è nullo, textField.text.lenght == 0. Quindi è possibile ridurre il se a: 'if (textField.text.lenght> 0)' –

+0

Entrambe le condizioni dipendono reciprocamente. Mantieni una sola condizione. Da 0 a 0 = 0, 1 & 1 = 1 – Saranjith

6

Joshua ha la risposta giusta nel caso stretta, ma in generale, non è possibile confrontare gli oggetti stringa usando il == o! = operatori. È necessario utilizzare -isEqual: o -isEqualToString: Questo perché charlieImputSelf e @"" sono effettivamente puntatori agli oggetti. Sebbene le due sequenze di caratteri possano essere uguali, non devono puntare alla stessa posizione in memoria.

+3

Quindi, se vuoi controllare se il valore del campo di testo è uguale a 'Boo', dovresti usare' if ([textField.text isEqualToString: @ "Boo"]) 'invece di' if (textField.text == @ "Boo") ' –

+0

Sì (è fastidioso Non posso inserire 'sì' in questa casella di testo) – JeremyP

2

Questi "funzionano" in un modo. Tuttavia, ho scoperto che l'utente può semplicemente compilare la casella con spazi. Ho trovato che usare le espressioni regolari aiuta (anche se quello che uso è per le parole senza spazi) non riesco a capire come rendere gli spazi.

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

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

Abbiamo già un metodo integrato che restituisce il valore booleano che indica se gli oggetti di immissione del testo hanno o meno testo.

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

Questo dovrebbe essere corretto (accettato) risposta –

+0

Non sapevo di hasText! – DogCoffee

+0

let clientName = clientNameTF.hasText()? clientNameTF.text! : "Nome cliente" – DogCoffee

1

il modo più efficiente per farlo è quello di utilizzare questo

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

Controllare se campo di testo è vuoto a 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 
    } 
    }