如何使用objective-c从UITabbarcontroller视图中删除子视图

时间:2013-09-11 07:12:54

标签: iphone ios objective-c

我在使用For循环的方法中将UILabel对象添加到标签栏控制器视图,但在另一种方法中,我需要从tabbarcontroller视图中删除所有UILabel子视图。

以下是我添加的代码:

-(void)tabBarImage_methodAdding:(NSNotification *)note
{
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    for (int i=0; i<4; i++)
    {
        UILabel *objLabel=[[UILabel alloc]initWithFrame:CGRectMake(18+80*i,            
                                      screenRect.size.height-18, 70, 15)];
        objLabel.backgroundColor=[UIColor clearColor];
        objLabel.text=[tabBarNamesArray objectAtIndex:i];
        objLabel.font=[UIFont systemFontOfSize:11.0];
        objLabel.textColor=[UIColor whiteColor];
        [self.tabBarController.view addSubview:objLabel];
        [objLabel release];objLabel=nil;
    }
}

以下是我的移除代码:

-(void)tabBarImage_methodRemoving:(NSNotification *)note
{
    for (UILabel *lab in self.tabBarController.view)
    {
        [lab removeFromSuperview];
    }
}

5 个答案:

答案 0 :(得分:1)

尝试这样的事情:

for (id subview in self.tabBarController.view.subviews) {
        if ([subview isMemberOfClass:[UILabel class]]) {
            [subview removeFromSuperview];
        }
    }

答案 1 :(得分:0)

if(self.tabBarController!=nil){
    while ([self.tabBarController.subviews count] > 0) {
        NSLog(@"subviews Count=%d",[[self.tabBarController subviews]count]);
        [[[self.tabBarController subviews] objectAtIndex:0] removeFromSuperview];
    }
}

答案 2 :(得分:0)

-(void)tabBarImage_methodRemoving:(NSNotification *)note
{
    for (id obj in self.tabBarController.view.subviews)
    {
        if([obj isKindOfClass:[UILabel class]]){
         [obj removeFromSuperview];   
        }
    }
}

答案 3 :(得分:-1)

For循环中尝试此操作: - [self.tabBarController.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

答案 4 :(得分:-1)

当您将UILabel添加到tabbarcontroller视图时,请获取tag.i.e

的帮助
   - (void)tabBarImage_methodAdding:(NSNotification *)note
    {
       CGRect screenRect = [[UIScreen mainScreen] bounds];
       for (int i=0; i<4; i++)
       {
          UILabel *objLabel=[[UILabel alloc]initWithFrame:CGRectMake(18+80*i,            
                                      screenRect.size.height-18, 70, 15)];
          objLabel.backgroundColor=[UIColor clearColor];
          objLabel.text=[tabBarNamesArray objectAtIndex:i];
          [objLabel setTag:1000+i];
          objLabel.font=[UIFont systemFontOfSize:11.0];
          objLabel.textColor=[UIColor whiteColor];
          [self.tabBarController.view addSubview:objLabel];
          [objLabel release];objLabel=nil;
      }

  - (void)tabBarImage_methodRemoving:(NSNotification *)note
   {
       for (UIView *lab in self.tabBarController.view.subviews)
       {
       if(lab.tag>=1000 && lab.tag<1004)
          [lab removeFromSuperview];
       }
   }