use*_*856 1 iphone nsstring viewdidload
我的应用程序在启动时检查NSString是否为空,如下所示:
if ([checkfieldstring1 isEqualToString:@""]) {
checkboxButton.hidden = YES;
}
else {
checkboxbutton.hidden = NO;
}
Run Code Online (Sandbox Code Playgroud)
但是,当存在空字符串时,该按钮不会被隐藏.我知道这个方法在我将它作为IBAction挂钩到按钮时有效,但在ViewDidLoad上没有...
在视图即将加载时检查空字符串需要检查nil字符串或空字符串,因为该字符串可能尚未设置.
if (checkfieldstring1 == nil || [checkfieldstring1 length] == 0) {
checkboxButton.hidden = YES;
} else {
checkboxButton.hidden = NO;
}
Run Code Online (Sandbox Code Playgroud)
或者,如果你像我一样这样做,并将其添加到我添加到项目中的一组常见宏中:
static inline BOOL isEmpty(id thing)) {
return thing == nil
|| ([thing respondsToSelector:@selector(length)]
&& [(NSData *)thing length] == 0)
|| ([thing respondsToSelector:@selector(count)]
&& [(NSArray *)thing count] == 0);
}
Run Code Online (Sandbox Code Playgroud)
由Wil Shipley提供http://www.wilshipley.com/blog/2005/10/pimp-my-code-interlude-free-code.html
你可以检查一下:
if(isEmpty(checkfieldstring1) {
checkboxButton.hidden = YES;
} else {
checkboxButton.hidden = NO;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
264 次 |
| 最近记录: |