簡體   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