My app checks if an NSString is empty when it launches, like this:
if ([checkfieldstring1 isEqualToString:@""]) {
checkboxButton.hidden = YES;
}
else {
checkboxbutton.hidden = NO;
}
However, when there is an empty string, the button is not being hidden. I know this method works when I hook it up as an IBAction to a button, but not on ViewDidLoad...
Checking for an empty string when the view is about to load needs to check for a nil string or an empty string as the string may not have been set up yet.
if (checkfieldstring1 == nil || [checkfieldstring1 length] == 0) {
checkboxButton.hidden = YES;
} else {
checkboxButton.hidden = NO;
}
Or, if you do as I do and have this in a set of common macros that I add to a project:
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);
}
Courtesy of Wil Shipley http://www.wilshipley.com/blog/2005/10/pimp-my-code-interlude-free-code.html
you could just check with:
if(isEmpty(checkfieldstring1) {
checkboxButton.hidden = YES;
} else {
checkboxButton.hidden = NO;
}