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.
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 */
}
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 */
}
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.
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 //
}
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
}
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
}
}
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 */
}
-(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];
}
}
精彩评论