我想花几个小时左右的按钮正常工作,并模仿后退按钮。UIBarButtonItem初始化与自定义视图选择器不能正常工作
我的代码来创建按钮:
UIBarButtonItem *backButton = [self customBarButton:@"back_button" imageHiglighted:@"settings_button_highlighted" x:20 y:0 widthDivider:2.6 heightDivider:2.6];
backButton.target = self;
backButton.action = @selector(buttonPressed:);
self.navigationItem.leftBarButtonItem = backButton;
这里所谓的创建自定义按钮的方法:
- (UIBarButtonItem *)customBarButton:(NSString *)imageDefault imageHiglighted:(NSString *)imageHighlighted x:(float)x y:(float)y widthDivider:(float)widthDivider heightDivider:(float)heightDivider {
UIImage *customImageDefault = [UIImage imageNamed:imageDefault];
UIImage *customImageHighlighted = [UIImage imageNamed:imageHighlighted];
CGRect frameCustomButton = CGRectMake(x, y, customImageDefault.size.width/widthDivider, customImageDefault.size.height/heightDivider);
UIButton *customButton = [[UIButton alloc] initWithFrame:frameCustomButton];
[customButton setBackgroundImage:customImageDefault forState:UIControlStateNormal];
[customButton setBackgroundImage:customImageHighlighted forState:UIControlStateHighlighted];
UIBarButtonItem *barCustomButton =[[UIBarButtonItem alloc] initWithCustomView:customButton];
return barCustomButton;
}
和动作:
-(void)buttonPressed:(id) sender{
NSLog(@"Entered");
SearchViewController *ViewController = [[SearchViewController alloc] init];
[self.navigationController pushViewController:ViewController animated:YES];
}
所以我能使用简单的UIButton而不是使用UIButtonBarItem,我真的不知道它是怎么回事。
如果你能帮助我,我会非常感激。
谢谢。
的解决方案是增加: [的CustomButton addTarget:自我行动:@selector(buttonPressed :) forControlEvents:UIControlEventTouchUpInside] ; 在我打电话的方法里面。 –