让我印象深刻的是,创造了一个新的CustomTabBarController
对于每个视图控制器来说都是根本错误的,特别是如果它是为了解决右键没有出现的问题。问题是您正在创建新的虚拟选项卡栏控制器,更糟糕的是,丢失了对视图控制器的真实选项卡栏控制器的引用。
您的解决方案触及了问题的核心,那就是,因为您正在为按钮添加子视图,即创建UIBarButtonItem
with initWithCustomView
等,您不能与多个导航控制器共享按钮。如果您创建一个简单的,则共享的公共右栏按钮将起作用UIBarButtonItem
with initWithTitle
(或其他类似的简单变体),但如果您使用按钮的自定义视图来执行此操作,则它不起作用。
正确的解决方案是创建一个新的UIBarButtonItem
具有您需要的行为和外观的实例。然后,任何需要该栏按钮项的视图控制器都可以创建该按钮的实例。于是,新习俗RightBarButtonItem
可能看起来像:
RightBarButtonItem.h:
@interface RightBarButtonItem : UIBarButtonItem
- (id)initWithTarget:(id)target action:(SEL)action;
@end
RightBarButtonItem.m:
@interface RightBarButtonItem ()
@property (nonatomic, strong) EZBadgeView *badgeView;
@end
@implementation RightBarButtonItem
- (id)initWithTarget:(id)target action:(SEL)action
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 46, 30);
self = [super initWithCustomView:button];
if (self)
{
_badgeView = [[EZBadgeView alloc] init];
CGRect badgeFrame = self.badgeView.frame;
badgeFrame.origin.x = 31.0f;
badgeFrame.origin.y = -6.0f;
badgeFrame = CGRectIntegral(badgeFrame);
_badgeView.frame = badgeFrame;
_badgeView.badgeBackgroundColor = [self colorWithRGBHex:kRed withAlpha:1.0f];
_badgeView.userInteractionEnabled = NO;
_badgeView.badgeTextFont = [UIFont fontWithName:@"BrownStd-Bold" size:12];
_badgeView.shouldShowGradient = NO;
_badgeView.shouldShowShine = NO;
[button setBackgroundImage:[UIImage imageNamed:@"button_mixer.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:@"button_mixer_pressed.png"] forState:UIControlStateHighlighted];
[button setBackgroundImage:[UIImage imageNamed:@"button_mixer_active.png"] forState:UIControlStateSelected];
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundColor:[UIColor clearColor]];
[button addSubview:_badgeView]; //Add NKNumberBadgeView as a subview on UIButton
_badgeView.badgeValue = @"0";
_badgeView.hidden = YES;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(updateBadgeNumber:)
name:@"updateBadgeNumber"
object:nil];
}
return self;
}
- (void)dealloc
{
// don't forget to have mechanism to remove the observer when this button is deallocated
[[NSNotificationCenter defaultCenter] removeObserver:self
name:@"updateBadgeNumber"
object:nil];
}
- (void)updateBadgeNumber:(NSMutableArray *)soundArray
{
self.badgeView.badgeValue = [NSString stringWithFormat:@"%i",[soundArray count]];
self.badgeView.hidden = ([soundArray count] == 0);
}
- (UIColor *)colorWithRGBHex:(UInt32)hex withAlpha:(float)alpha
{
int r = (hex >> 16) & 0xFF;
int g = (hex >> 8) & 0xFF;
int b = (hex) & 0xFF;
return [UIColor colorWithRed:r / 255.0f
green:g / 255.0f
blue:b / 255.0f
alpha:alpha];
}
@end
然后,CustomTabBarController
大大简化了:
自定义TabBarController.h:
@interface CustomTabBarController : UITabBarController
- (UIBarButtonItem *)rightBarButton;
@end
自定义TabBarController.m:
#import "RightBarButtonItem.h"
@implementation CustomTabBarController
- (UIBarButtonItem *)rightBarButton
{
return [[RightBarButtonItem alloc] initWithTarget:self
action:@selector(clickedTest:)];
}
- (void)clickedTest:(id)sender
{
NSLog(@"%s", __FUNCTION__);
}
@end
最后,添加到选项卡栏控制器的所有视图控制器都可以简单地执行以下操作:
- (void)viewDidLoad
{
[super viewDidLoad];
CustomTabBarController *tabBarController = (id)self.tabBarController;
self.navigationItem.rightBarButtonItem = [tabBarController rightBarButton];
}
所有这些都实现了您提出的解决方案的关键部分,即确保我们拥有右侧栏按钮项目的唯一实例,但不存在创建冗余选项卡栏控制器的问题。