Reputation: 5559
I want to pass in various custom class objects into a method and for the method to check if the object has any content (i.e. a custom label has no text or an image view has no image etc). Is this possible?
Upvotes: 0
Views: 24
Reputation: 12093
You could do something along the lines of
- (BOOL)viewObjectContainsContent:(id)object
{
BOOL containsContent = false;
// First check that the object passed in isn't nil no point doing anything otherwise.
// We also want to check that the object is an instance of UIView or a subclass of it.
if(object && [object isKindOfClass:[UIView class]]) {
// Not going to do all the different combinations but you'll get what's going on
if ([object isMemberOfClass:[UILabel class]]) {
UILabel *labelObject = (UILabel *)object;
if ([labelObject text]) containsContent = true;
} else if ([object isMemberOfClass:[UIImageView]]) {
UIImageView *imageObject = (UIImageView*)object;
if ([imageObject image]) containsContent = true;
} else if (//So on checking the different objects.....
}
return containsContent;
}
then you could call it using BOOL boolVal = [self viewObjectContainsContent:label];
self
being the instance of a class that this method is part of.
Also note that if you are using subclasses of things like UILabel
and UIImageView
you may need to replace isMemberOfClass:
with the isKindOfClass:
to check subclasses as well.
Hope this is what you are looking for if not leave a comment and I will amend accordingly.
Upvotes: 1
Reputation: 2521
Yu can't do that if you don't know the object. The label and the imageView (the same for other objects) don't share the property name to make this kind of check possible. What you can do is to check if the object is nil.. or write a method that checks if the object is the kind of each one of the classes you want to test, cast and then check the property.
Understand that what you are calling empty is not the same for all objects.
Upvotes: 1