我在从superview中删除视图时遇到问题。 添加视图:
- (void)createCircles
{
NSString *currentDate = [self currentDate];
NSArray *array = [self.horizontalScroll subviews];
UILabel *label = nil;
for (label in array)
{
if ([label.text isEqualToString:currentDate])
{
UIView *view = [[UIView alloc] initWithFrame:label.frame];
view.backgroundColor = [UIColor redColor];
[self.horizontalScroll insertSubview:view atIndex:0];
[self.labelsArray insertObject:view atIndex:0];
}
}
}
尝试删除:
- (void)labelTouch:(UITapGestureRecognizer*)sender
{
NSArray *array = [self.horizontalScroll subviews];
UILabel *label = (UILabel*)sender.view;
for (int i = 0; i < [array count]; ++i)
{
UILabel *l = array[i];
if (label.tag == l.tag)
{
UIView *view = nil;
view = [self.labelsArray objectAtIndex:0];
view.hidden = YES;
[view removeFromSuperview];
view = nil;
[self.labelsArray removeObjectAtIndex:0];
}
}
}
但触摸视图仍然显示后。试图删除标签(l) - 它被删除
答案 0 :(得分:1)
试试这个,
[[[self.horizontalScroll subviews] objectAtIndex:0] removeFromSuperView];
答案 1 :(得分:0)
您应该在ivar或property中存储对此“unkillable”视图的引用。在第一个方法中初始化它并在第二个方法中调用removeFromSupperView。