微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

iphone – UIBarButtonItem init,自定义视图选择器无法正常工作

我正在努力让左按钮正常工作并模仿后退按钮.

我创建按钮的代码

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来制作它,我真的不知道它是怎么回事.

如果你能帮助我,我会非常感激.

谢谢.

解决方法

这样做会将选择器添加自定义按钮,因为它是bar buttom的视图:

[customButton addTarget:self action:@selector(buttonpressed:) forControlEvents:UIControlEventTouchUpInside];

编辑:注意:UIBarButtonItem的目标和操作适用于自定义视图.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐