-1

我最近一直在 Xcode 工作。我使用了一个带有 4 个文本框的表格视图,在一个按钮上单击我正在检查是否有任何文本框具有空值,如果没有将其保存在本地文件中。

代码如下

NSString *FName, *LName, *Email, *Pwd;

FName=registerFirstName.text;
LName=registerLastName.text;
Email=registerEmail.text;
Pwd=registerPassword.text;

BOOL fileexists=[[NSFileManager defaultManager]fileExistsAtPath:storePath];
if(!fileexists)
    [storeManager createFileAtPath:storePath contents:nil attributes:nil];

if (FName !=NULL && LName != NULL && Email != NULL && Pwd != NULL && FName!=@"" && LName!=@"" && Email!=@"" && Pwd!=@"") {

    UIAlertView *missingInfo=[[UIAlertView alloc]initWithTitle:@"Missing Details" message:@"All Fields Are Necessary" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
    [missingInfo show];
  } else {
    NSString *fullUserInfo;
    fullUserInfo=[NSString stringWithFormat:@"First Name:\t%@\nLast Name:\t%@\nEmail ID:\t%@\nPassword:\t%@",FName,LName,Email,Pwd];
    [fullUserInfo writeToFile:storePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
    UIAlertView *test=[[UIAlertView alloc]initWithTitle:@"InfoSaved" message:fullUserInfo delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
    [test show];
}

当我在模拟器中运行它而不输入任何值时,它确实可以显示缺少字段的警报。但是当我在文本框中输入一些值然后删除它时,如果我单击完成以保存它,它会接受空值并将其存储到我不希望发生的情况。我的代码中的逻辑有什么问题,还是 xcode 中默认有其他函数来检查 null?

4

5 回答 5

3
if (textfieldName.text.length == 0) {
    NSLog(@"Text field empty");
} else {
    NSLog(@"Text field has value");
}
于 2012-12-19T07:35:47.827 回答
2

与 NULL 或 nil 比较不会给您预期的结果....要检查字符串是否为空,您必须使用它...

if([Fname isEqualtoString = @""] &&.....)
{
   UIAlertView *missingInfo=[[UIAlertView alloc]initWithTitle:@"Missing Details" message:@"All Fields Are Necessary" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
    [missingInfo show];
}
else
{
//put your code here.
}

我敢肯定...这会对您有很大帮助...干杯...

于 2012-12-19T07:26:05.760 回答
1

对于 MacOS 应用程序

if([_text.stringValue isNotEqualTo:@""]){
    NSLog(@"contains");
}
else{
    NSLog(@"empty");
}

编辑:

对于 iOS:

NSString *textString=_text.text;

if(textString==NULL){
    NSLog(@"NULL");
}
else{
    NSLog(@"contains");
}
于 2012-12-19T07:17:32.617 回答
0

你可以这样做:

if ([myString isEqualtoString:@""])
{

}
else
{

}
于 2014-06-09T15:59:49.093 回答
-2
if(Fname.length==0)
{
  NSLog(@"null");
}
于 2012-12-19T07:32:09.883 回答