繁体   English   中英

如何使用Objective-C从UITabbarcontroller视图中删除子视图

[英]How to remove subviews from UITabbarcontroller view using objective-c

我在使用For Loop的方法中将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];
    }
}

尝试这样的事情:

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)];

将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];
       }
   }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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