1

我在使用 Loop 的方法中将 UILabel 对象添加到选项卡栏控制器视图For,但在另一种方法中,我需要从 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];
    }
}
4

5 回答 5

1

尝试这样的事情:

for (id subview in self.tabBarController.view.subviews) {
        if ([subview isMemberOfClass:[UILabel class]]) {
            [subview removeFromSuperview];
        }
    }
于 2013-09-11T07:17:31.360 回答
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];
    }
}
于 2013-09-11T07:17:23.600 回答
0
-(void)tabBarImage_methodRemoving:(NSNotification *)note
{
    for (id obj in self.tabBarController.view.subviews)
    {
        if([obj isKindOfClass:[UILabel class]]){
         [obj removeFromSuperview];   
        }
    }
}
于 2013-09-11T07:54:58.460 回答
-1

For在你的循环中试试这个: -
[self.tabBarController.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

于 2013-09-11T07:16:31.697 回答
-1

当您将 UILabel 添加到 tabbarcontroller 视图时,请借助 tag.ie

   - (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];
       }
   }
于 2013-09-11T07:19:07.153 回答