1

我使用带有此代码的 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 时,我不希望显示徽章。如何从视图中删除或隐藏它。

谢谢

4

2 回答 2

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:...].

希望这可以帮助

于 2012-10-08T17:15:25.323 回答
0

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

于 2012-10-08T17:15:50.837 回答