我在 UIBarButtonItem 上使用这个类别方法来创建自定义按钮,如下所示:
+ (UIBarButtonItem*)itemWithImage:(UIImage*)image forState:(UIControlState)controlState target:(id)target action:(SEL)action{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:image forState:controlState];
button.frame= CGRectMake(0.0, 0.0, 44, 44);
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
UIView *v=[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 44, 44) ];
[v addSubview:button];
return [[UIBarButtonItem alloc] initWithCustomView:button];
}
然后我创建按钮并将它们分配给我的视图控制器中的导航项,如下所示:
-(void)viewDidLoad{
[super viewDidLoad];
UIBarButtonItem * cancelButtonItem = [UIBarButtonItem itemWithImage:[UIImage imageNamed:@"Cancel"] forState:UIControlStateNormal target:self action:@selector(cancel)];
self.navigationItem.leftBarButtonItem = cancelButtonItem;
UIBarButtonItem * checkmarkButtonItem = [UIBarButtonItem itemWithImage:[UIImage imageNamed:@"checkmark_active"] forState:UIControlStateNormal target:self action:@selector(done)];
self.navigationItem.rightBarButtonItem = checkmarkButtonItem;
}
我第一次创建视图控制器并按下它时,按钮可以工作,但是当创建一个全新的视图控制器并将其推送到导航堆栈时,它会中断。有任何想法吗?我已经彻底调试了这个并且没有想法。