我想将各种自定义类对象传递给方法,并且方法检查对象是否有任何内容(即自定义标签没有文本或图像视图没有图像等)。这可能吗?
答案 0 :(得分:1)
了解您所调用的空白对于所有对象都不相同。
答案 1 :(得分:1)
你可以按照
的方式做点什么- (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;
}
然后你可以使用BOOL boolVal = [self viewObjectContainsContent:label];
self
作为该方法所属类的实例来调用它。
另请注意,如果您使用UILabel
和UIImageView
之类的子类,则可能需要将isMemberOfClass:
替换为isKindOfClass:
以检查子类。
希望如果不发表评论,这就是你要找的东西,我会相应修改。