简体   繁体   中英

How to remove subviews from UITabbarcontroller view using objective-c

I added UILabel object to the tab bar controller view in a method using For Loop, but in another method I need to remove all UILabel sub views from tabbarcontroller view.

Here is my code for adding:

-(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;
    }
}

Here is my code for removing:

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

Try something like this:

for (id subview in self.tabBarController.view.subviews) {
        if ([subview isMemberOfClass:[UILabel class]]) {
            [subview removeFromSuperview];
        }
    }
if(self.tabBarController!=nil){
    while ([self.tabBarController.subviews count] > 0) {
        NSLog(@"subviews Count=%d",[[self.tabBarController subviews]count]);
        [[[self.tabBarController subviews] objectAtIndex:0] removeFromSuperview];
    }
}
-(void)tabBarImage_methodRemoving:(NSNotification *)note
{
    for (id obj in self.tabBarController.view.subviews)
    {
        if([obj isKindOfClass:[UILabel class]]){
         [obj removeFromSuperview];   
        }
    }
}

在您的For循环中尝试以下操作:-
[self.tabBarController.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

When you are adding UILabels to the tabbarcontroller view take help of 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];
       }
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM