删除/隐藏CustomBadge

时间:2012-10-08 17:05:31

标签: iphone ios

我使用CustomBadge使用以下代码显示徽章:

-(void)viewWillAppear:(BOOL)animated{


[self fillBadges];

//********** Start Custom Badge ***************//
NSString *myNewString = [NSString stringWithFormat:@"%i",allBadge];

if (allBadge >= 1) {
    NSLog(@"Add One");
    CustomBadge *customBadgeAll = [CustomBadge customBadgeWithString:myNewString];
    [customBadgeAll setFrame:CGRectMake(125, 90, customBadgeAll.frame.size.width, customBadgeAll.frame.size.height)];
    [self.view addSubview:customBadgeAll];      
}
//********** End Custom Badge ***************//


[self.view setNeedsDisplay];
[[self navigationController] setNavigationBarHidden:NO animated:NO];

}

当allBadge计数小于1时,我不希望徽章显示。如何从视图中删除或隐藏它。

由于

2 个答案:

答案 0 :(得分:0)

为customBadgeAll创建一个类范围的变量:

你的.h:

中的

CustomBadge *customBadgeAll;

然后您可以轻松地再次移除徽章:

if (allBadge >= 1) {
  NSLog(@"Add One");
  CustomBadge *customBadgeAll = [CustomBadge customBadgeWithString:myNewString];
  [customBadgeAll setFrame:CGRectMake(125, 90, customBadgeAll.frame.size.width, customBadgeAll.frame.size.height)];
  [self.view addSubview:customBadgeAll];      
}
else{
  [customBadgeAll removeFromSuperview];
}

另一种方法是将customBadgeAll的代码值设置为特殊值,然后使用[self.view viewWithTag:...]检索它。

希望这有帮助

答案 1 :(得分:0)

为什么不使用以下代码customBadgeAll.hidden = YES来隐藏它?