2013-02-27 67 views
-2

我很難從視圖中刪除所有UIButton。從視圖中刪除所有UIButtons?

我已經將它們添加到UIScrollView for循環中,稍後我需要刪除它們。

所以要加他們:(在cocos2d的場景)

sview = [[UIScrollView alloc] 
          initWithFrame:[[UIScreen mainScreen] bounds]]; 

...... 
for(int i =0; i<[assets count]-1; i++) 
    { 

     UIImage *thumb= [assets objectAtIndex:i]; 
     UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom]; 
       [sview addSubview:button]; 
....... 
[[[CCDirector sharedDirector] view] addSubview:sview]; 

,並刪除它們:

[((UIView *)sview) removeFromSuperview]; //which usually works but no now . 

我怎麼會在這一切的按鈕隨後運行並刪除它們? 我沒有聯繫他們,我想在視圖中的所有按鈕運行..

編輯:曾嘗試沒有成功

for (int i=0; i<[assets count];i++) 
    { 
     UIButton *myButton = (UIButton *)[sview viewWithTag:i]; 
     [((UIView *)myButton) removeFromSuperview]; 
    } 

回答

0

有幾個方法可以做到這一點陣列上 然後,只需迭代。有人提出了一種方法。我個人喜歡保留所有添加到NSMutableArray中的東西(將它們添加到視圖時添加到數組中),然後循環訪問數組以刪除它們。

for (... ; ... ; ...) { 
    UIButton *button = .... 
    // in your "add button loop" just record them in an array 
    [self.transientViews addObject:button]; 
} 


// remove them later with 
for (UIView *view in self.transientViews) 
    [view removeFromSuperview]; 
[self.transientViews removeAllObjects]; 

我喜歡這個,因爲它給了我更多的靈活性。我可能想要刪除它們或其他東西。他們可以是UIView的任何子類,我不必擔心它。

+0

非常感謝! – bilanbila 2013-02-27 16:07:37

+0

sview背景不會消失,但是所有的按鈕都會消失。我試圖把他帶走[((UIView *)sview)removeFromSuperview];哪些工作按鈕,但它仍然存在。我NSlog它看到我有一個鏈接給他,我得到他的價值觀。 – bilanbila 2013-02-27 16:12:59

1
for (UIView *subview in [((UIView *)sview).subviews copy]) { 
    if ([subview isKindOfClass:[UIButton class]]) 
     [subview removeFromSuperview]; 
} 
+0

不工作..... :( – bilanbila 2013-02-27 15:53:45

+0

@ user1994291停止抱怨並努力理解自己的代碼,然後將代碼添加到提供給其中一個地方的代碼中,然後以合理的方式進行操作。 – 2013-02-27 15:57:16

+0

我可以看到他們有一些不透明,我幾乎不能在背景中看到他們......這裏真的很糟糕...... – bilanbila 2013-02-27 15:58:37

2

在技術上是可行的,它不是這樣設計你的代碼是個好主意。

我沒有鏈接到他們

就出在這裏你的問題。在您創建並添加它們時將它們放入NSMutableArray,然後遍歷此數組以刪除它們。

但是,如果由於某種原因,你不這樣做,你可以檢查你的視圖的所有子視圖爲是一個的UIButton:

- (void)removeUIButtonsFromView:(UIView *v) 
{ 
    for (UIView *sub in v.subviews) { 
     if ([sub isKindOfClass:[UIButton class]]) { 
      [sub removeFromSuperview]; 
     } else { 
      [self removeUIButtonsFromView:sub]; 
     } 
    } 
} 
+0

我試過,我得到了for循環的警告:uiscrollview上的集合表達式類型可能不會響應countByEnumerating ...... – bilanbila 2013-02-27 15:49:25

+0

@ user1994291然後你搞砸了一些東西。 – 2013-02-27 15:50:40

+0

請注意,在可可中使用縮寫被認爲是不好的風格。 – 2013-02-27 15:51:32

1

如果它在滾動視圖僅按鈕,

[sview.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]; 
+0

'makeObjectsPerformSelector'確實是一個整潔的方法:) – Vinzzz 2013-02-27 15:49:30

+0

這可能不是OP的「正確答案」,但這是一個很好的竅門!謝謝! – bdesham 2013-02-27 15:52:09

+0

無法正常工作。他們留在那裏.. – bilanbila 2013-02-27 15:53:04

0
[((UIView *)sview) removeFromSuperview] 

你刪除了滾動sview:與將它們全部刪除,爲什麼?

當您添加按鈕時,只需將它們添加到您保留的NSArray屬性。 whenver要刪除它們

//in your interface 

@property (nonatomic, strong) NSArray *buttons; 

//in your implementation 

sview = [[UIScrollView alloc] 
          initWithFrame:[[UIScreen mainScreen] bounds]]; 

...... 
NSMutableArray *tempArray = [NSMutableArray array]; 
for(int i =0; i<[assets count]-1; i++) 
    { 

     UIImage *thumb= [assets objectAtIndex:i]; 
     UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom]; 
       [sview addSubview:button]; 
     [tempArray addObject:button]; 
    } 
    self.buttons = tempArray; 
....... 

// later, to remove all buttons 

- (void) removeButtons 
{ 
    for(UiButton *button in self.buttons){ 
     [button removeFromSuperview]; 
    } 
    self.buttons = nil; 
}