问题描述
我有一个从文本文件加载的UILabel。有时文本文件中包含某些内容,有时它是空的。所以有时候UILabel是空白的,有时它里面有文字。
I have a UILabel that is loaded form a text file. Sometimes the text file has something in it and sometimes it is empty. So sometimes the UILabel is blank and sometimes it has text in it.
我想编写一个if语句,说明如果UILabel是空白的话,如果它有文本,则做另外一件事。
I want to write an if statement that says if the UILabel is blank do one thing else if it has text in it do another thing.
我试过
if (self.label.text = NULL)
和
if (self.label.text = @"")
但它无法正常工作。
使用 if(self.label.text = @)
,我得到if语句,但是else语句不起作用。
With the if (self.label.text = @"")
, I get the if statement to happen but the else statement doesn't work.
这是我的代码
NSString *stuff3 = @"/Stuff";
NSString *titleName = [familyDictionary objectForKey:@"identity"];
NSArray *paths3 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory3 = [paths3 objectAtIndex:0];
NSString *stuffPath3 = [documentsDirectory3 stringByAppendingPathComponent:stuff3];
NSString *fullPath3 = [stuffPath3 stringByAppendingPathComponent:titleName];
self.title = [NSString stringWithContentsOfFile:fullPath3 encoding:NSUTF8StringEncoding error:NULL];
NSLog(@"full path 3 >>>%@",fullPath3);
NSString *stuff4 = @"/Stuff/Objects";
NSString *textName3 = [familyDictionary objectForKey:@"identity"];
NSArray *paths2 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory2 = [paths2 objectAtIndex:0];
NSString *stuffPath = [documentsDirectory2 stringByAppendingPathComponent:stuff4];
NSString *fullPath2 = [stuffPath stringByAppendingPathComponent:textName3];
self.wordlabel.text = [NSString stringWithContentsOfFile:fullPath2 encoding:NSUTF8StringEncoding error:NULL];
//Now load the image at fullPath and install it into our image view's image property.
NSLog(@"full path 3 >>>%@",fullPath2);
if(self.wordlabel.text = @"")
{
[textView setTitle:[NSString stringWithContentsOfFile:fullPath3 encoding:NSUTF8StringEncoding error:NULL] forState:UIControlStateNormal] ;
textView.titleLabel.adjustsFontSizeToFitWidth = TRUE;
}
else
{
[textView setTitle:[NSString stringWithContentsOfFile:fullPath2 encoding:NSUTF8StringEncoding error:NULL] forState:UIControlStateNormal] ;
textView.titleLabel.adjustsFontSizeToFitWidth = TRUE;
}
推荐答案
你在做的是比较指针,不适用于字符串。使用此
What you are doing is comparing pointers, which doesn't work with strings. Use this
if ([self.wordlabel.text isEqualToString:@"thestring"])
这篇关于如果声明空UILabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!