具有自定义视图select器的UIBarButtonItem init无法正常工作
我正在尝试几个小时,使一个左button正常工作,并模仿一个后退button。
我的代码来创buildbutton:
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;
这里调用的方法来创build自定义button:
- (UIBarButtonItem *)customBarButton:(NSString *)imageDefault imageHiglighted:(NSString *)imageHighlighted x:(float)xy:(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来实现,我真的不知道这是怎么回事。
如果你能帮助我,我会非常感激。
谢谢。
这样做是否将selector
添加到自定义button
因为它是bar
buttom的view
:
[customButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
编辑 : 注意 : UIBarButtonItem
的target
和action
适用于custom views
。
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setImage:[UIImage imageNamed:@"goback.png"] forState:UIControlStateNormal]; [button addTarget:self action:@selector(//HERE ! i don't know put what thing !) forControlEvents:UIControlEventTouchUpInside]; [button setFrame:CGRectMake(0, 0, 32, 32)]; self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
尝试这个
self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:[GlobalMethods buttonWithImage:@"top-nav-back" heighlightImageName:nil buttonFrame:CGRectMake(102, 1, 50, 30) selectorName:@selector(btnBackClicked:) target:self]]; + (UIButton*)buttonWithImage:(NSString*)normalImageName heighlightImageName:(NSString*)heighlightImageName buttonFrame:(CGRect)buttonFrame selectorName:(SEL)selectorName target:(id)target { UIButton *objButton = [UIButton buttonWithType:UIButtonTypeCustom]; [objButton setFrame:buttonFrame]; [objButton addTarget:target action:selectorName forControlEvents:UIControlEventTouchUpInside]; [objButton setImage:[GlobalMethods getImageFromResourceBundle:normalImageName] forState:UIControlStateNormal]; if(heighlightImageName) [objButton setImage:[GlobalMethods getImageFromResourceBundle:heighlightImageName] forState:UIControlStateDisabled]; if(heighlightImageName) [objButton setImage:[GlobalMethods getImageFromResourceBundle:heighlightImageName] forState:UIControlStateSelected]; return objButton; }
尝试这个..
[customButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
在你的customBarButton方法..我的意思是说select器给你的button,而不是酒吧button项..
检查并回复..
只要把这行代码放在代码中
[backButton addTarget:self action:@selector(backBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
这对我有效
UIButton * tmpButton = [self generateButtonwithImageName:@"Back.png" andSize:CGRectMake(0, 0, 55, 30)]; UIBarButtonItem * backButton = [[UIBarButtonItem alloc] initWithCustomView:tmpButton]; [tmpButton addTarget:self action:@selector(reload) forControlEvents:UIControlEventTouchUpInside]; self.navigationItem.leftBarButtonItem=backButton; -(UIButton*)generateButtonwithImageName :(NSString*)imageName andSize:(CGRect)rect{ UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame=rect; [button setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal]; return button; }
//为我工作
if([version floatValue] < 7.0) { [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navi-bar.png"] forBarMetrics:UIBarMetricsDefault]; self.navigationController.navigationBar.opaque = YES; closeButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 35, 30)]; [closeButton setImage:[UIImage imageNamed:@"btn_end.png"] forState:UIControlStateNormal]; [closeButton setImage:[UIImage imageNamed:@"btn_end_on.png"] forState:UIControlStateHighlighted]; [closeButton addTarget:self action:@selector(actionClose) forControlEvents:UIControlEventTouchUpInside]; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:closeButton]; } else{ /*add button back to navigation*//*add buttom back to navigation*/ UIBarButtonItem *itemBack = [[UIBarButtonItem alloc]initWithTitle:@"完了" style:UIBarButtonItemStylePlain target:self action:@selector(actionClose)]; self.navigationItem.rightBarButtonItem = itemBack; }