使用UIViews和UIImageViews数组([[[UIApplication sharedApplication] window]子视图])。我只需要删除UIImageView类型的最高索引的对象。
答案 0 :(得分:6)
您可以使用indexOfObjectWithOptions:passingTest:
方法反向搜索使用块传递测试的对象,然后在结果位置删除该对象:
NSUInteger pos = [myArray indexOfObjectWithOptions:NSEnumerationReverse
passingTest:^(id obj, NSUInteger idx, BOOL *stop) {
return [obj isKindOfClass:[UIImageView class]]; // <<== EDIT (Thanks, Nick Lockwood!)
}];
if (pos != NSNotFound) {
[myArray removeObjectAtIndex:pos];
}
答案 1 :(得分:5)
另一个基于块的解决方案
[window.subviews enumerateObjectsWithOptions:NSEnumerationReverse
usingBlock:^(id view, NSUInteger idx, BOOL *stop)
{
if ([view isKindOfClass:[UIImageView class]]){
[view removeFromSuperview];
*stop=YES;
}
}];
非阻止解决方案:
for (UIView *view in [window.subview reverseObjectEnumerator])
{
if ([view isKindOfClass:[UIImageView class]]){
[view removeFromSuperview];
break;
}
}
我发布了some demo code,其中显示了两种解决方案。
答案 2 :(得分:3)
怎么样:
UIWindow *window = [[UIApplication sharedApplication] window];
UIView *imageView = nil;
for (UIView *view in window.subviews)
{
if ([view isKindOfClass:[UIImageView class]])
{
imageView = view;
}
}
//this will be the last imageView we found
[imageView removeFromSuperview];