2
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    NSLog(@"will rotation");

    for (UIButton *button in self.view.subviews) {
        [button removeFromSuperview];
    }

}

このコードに問題があります。ビューからUIButtonのみを削除する必要があります。しかし、このコードは私のself.viewのすべてのサブビューも削除します。どうすればこれを解決できますか?

4

5 に答える 5

5

これを行う:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
NSLog(@"will rotation");

   for (id subview in self.view.subviews) {
    if([subview isKindOfClass:[UIButton class]]) //remove only buttons
    {
      [subview removeFromSuperview];
    }
   }

}
于 2012-08-20T14:35:44.653 に答える
4

UIButtonを反復するのではなく、すべてのビューを反復してUIButtonにキャストしています。これを試して:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    NSLog(@"will rotation");

    for (UIView *button in self.view.subviews) {
        if ([button isKindOfClass:[UIButton class]]) {
            [button removeFromSuperview];
        }        
    }
}
于 2012-08-20T14:36:14.627 に答える
3
for (id button in self.view.subviews) 
    {
        if(button isKindOfClass:[UIButton class])
        {
          [button removeFromSuperview];
        }
    }
于 2012-08-20T14:36:00.177 に答える
2

self.view.subviewsすべてのサブビューをフェッチしUIButton *、そのように型を使用してもリストはフィルター処理されません。UIButton のようなオブジェクトを処理できると予想されることをコンパイラーに示唆するだけです。次のように、それぞれを検査する必要があります。

for (UIView *subview in self.view.subviews) {
    if([subview isKinfOfClass:[UIButton class]])
      [subview removeFromSuperview];
}
于 2012-08-20T14:38:02.450 に答える
0

1 つのオプションは、ファイルの先頭にタグを作成することです。

#define BUTTONSTODELETE 123

次に、それぞれを設定しますbuttons.tag = BUTTONSTODELETE

ついに :

for (UIButton *v in [self.view subviews]){
        if (v.tag == BUTTONSTODELETE) {
            [v removeFromSuperview];

そのタグが付いたアイテムのみが削除されることを保証します

EDIT:Maulikとfbernardoには、より良い、面倒でない方法があります

于 2012-08-20T14:37:50.313 に答える