使用自定义视图选择器的UIBarButtonItem初始化无法正常工作 [英] UIBarButtonItem init with custom view selector not working properly

查看:46
本文介绍了使用自定义视图选择器的UIBarButtonItem初始化无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力几个小时,以使向左按钮正常工作并模仿向后按钮.

I'm trying for hours to make a left button work properly and mimic a back 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;

}

动作:

-(void)buttonPressed:(id) sender{

    NSLog(@"Entered");
    SearchViewController *ViewController = [[SearchViewController alloc] init];
    [self.navigationController pushViewController:ViewController animated:YES];

}

所以我能够用一个简单的UIButton而不是UIButtonBarItem来实现它,我真的不知道它是怎么回事.

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.

如果您能帮助我,我将非常感激.

If you could help me I'd be very grateful.

谢谢.

推荐答案

为此将选择器添加到自定义 button 中,因为它是 view bar 按钮:

Do this add selector to custom button as it is view of bar buttom:

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

编辑:注意: UIBarButtonItem target action 自定义视图.

这篇关于使用自定义视图选择器的UIBarButtonItem初始化无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆