使用自定义视图选择器的UIBarButtonItem初始化无法正常工作

使用自定义视图选择器的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 自定义视图.