xcode 带有自定义视图选择器的 UIBarButtonItem init 无法正常工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12137345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIBarButtonItem init with custom view selector not working properly
提问by Roger Fernandez Guri
I'm trying for hours to make a left button work properly and mimic a back button.
我花了几个小时试图让左键正常工作并模仿后退键。
My code to create the button:
我创建按钮的代码:
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;
Here the method called to create custom button:
这里调用了创建自定义按钮的方法:
- (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;
}
And the action:
和行动:
-(void)buttonPressed:(id) sender{
NSLog(@"Entered");
SearchViewController *ViewController = [[SearchViewController alloc] init];
[self.navigationController pushViewController:ViewController animated:YES];
}
So I was able to make it with a simple UIButton but not with a UIButtonBarItem and I really don't know what's going on with it.
所以我能够用一个简单的 UIButton 但不能用 UIButtonBarItem 来制作它,我真的不知道它发生了什么。
If you could help me I'd be very grateful.
如果你能帮助我,我将不胜感激。
Thanks.
谢谢。
回答by Paresh Navadiya
Do this add selector
to custom button
as it is view
of bar
buttom:
做到这一点的附加selector
定制button
,因为它是view
的bar
BUTTOM:
[customButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
EDIT: Note: the target
and action
of the UIBarButtonItem
apply to custom views
.
编辑:注:该target
和action
的UIBarButtonItem
申请custom views
。
回答by Vikas S Singh
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];
回答by aViNaSh
TRY THIS
尝试这个
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;
}
回答by amitkumar3968
Try this..
尝试这个..
[customButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
in your customBarButton method.. I mean to say give selector to your button instead of bar button item..
在你的 customBarButton 方法中.. 我的意思是说给你的按钮选择器而不是栏按钮项目..
Check and reply..
检查并回复..
回答by Rajneesh071
Just put this line in your code..
只需将此行放在您的代码中..
[backButton addTarget:self action:@selector(backBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
回答by Waris Ali
This worked for me
这对我有用
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;
}
回答by ThangNguyen
//working for me
//对我来说有效
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;
}