簡體   English   中英

如何從子視圖中刪除nsmutablearray的對象?

[英]How to remove the nsmutablearray's object from subview?

我有nsmutablearray,我在其中保存了一些字符,表示字母,現在我想要的是將這些對象顯示為屏幕上的子視圖。 我很容易做到..並且通過循環來做到這一點..我正在一個一個地訪問數組對象,通過它我可以將它們添加為子視圖,但是現在em無法從超級視圖中刪除它們。 我要刪除這些標簽。 我怎樣才能做到這一點?

UILabel *myLabel;
UIImageView *image;

for (int j = 0; j<[intValues count];j++) {
   image = [allGridImages objectAtIndex:j];
    image.userInteractionEnabled=NO;
    image.multipleTouchEnabled=NO;
    image.image= [UIImage imageNamed:@"box-1.png"];

    title = [allGridBoxesTitle objectAtIndex:j];
    myLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 1, 45, 45)];
    myLabel.text = [allGridBoxesTitle objectAtIndex:j];
    myLabel.textColor = [UIColor grayColor];
    myLabel.font = [UIFont boldSystemFontOfSize:26.0f];
    myLabel.backgroundColor = [UIColor clearColor];
    [image addSubview:myLabel];

試試這個方法

- (void)removeAllLabels
{
    for (UIView *view in self.view.subviews)
    {
        if ([view isKindOfClass:[UILabel class]])
        {
            [view removeFromSuperview];
        }
    }
}

您可以在添加新標簽之前執行此操作:

[image.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

這將指示所有子視圖將其刪除。

但是,這取決於以下事實: UIImageView在當前版本的iOS中沒有任何子視圖,而這不一定總是正確的。 最好實際跟蹤這些標簽,並在使用完它們后專門刪除它們,甚至最好重用它們,而不是每次都制作新標簽。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM