Objective c 如果使用uiBarButtonim:initWithImage:但未使用uiBarButtonim:initWithCustomView:则调用方法
我有一个方法,当用户单击导航栏中的按钮时,我希望调用该方法。如果像这样添加按钮,则会调用我的方法:Objective c 如果使用uiBarButtonim:initWithImage:但未使用uiBarButtonim:initWithCustomView:则调用方法,objective-c,ios,Objective C,Ios,我有一个方法,当用户单击导航栏中的按钮时,我希望调用该方法。如果像这样添加按钮,则会调用我的方法: UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"myEditButton"] style:UIBarButtonItemStyl
UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"myEditButton"]
style:UIBarButtonItemStyleBordered
target:self
action: @selector(enterEditMode:)];
editButton.title = @"Edit";
[self.navigationItem setRightBarButtonItem:editButton animated:YES];
当用户单击该按钮时,将调用我的enterEditMode:方法
然而,使用此代码的结果看起来就像附件中一样-没有文本,但更糟糕的是,我的图像位于蓝色按钮的顶部。我不能使用标准系统编辑按钮,因为有一个要求它是黑色而不是蓝色的,而且AFAIA我无法将标准编辑栏按钮的颜色更改为黑色
因此,在xib中,我创建了一个父UIView,其中包含一个UIImageView,它是按钮图像和文本的UILabel。
然后我创建如下按钮项:
UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithCustomView:self.editButtonParentView];
[editButton setTarget:self];
[editButton setAction: @selector(enterEditMode:)];
editButton.style = UIBarButtonItemStyleBordered;
editButton.target = self;
self.editButtonLabel.text = @"Edit"
[self.navigationItem setRightBarButtonItem:editButton animated:YES];
其中editButtonParentView是nib中partent视图的IBOutlet
这显示得非常完美,但问题是当用户单击enterEditMode:时,它不会被调用
为什么不打电话
谢谢
在这里,我发现了如何获得与系统“编辑”按钮相同的外观,但可以获得所需的任何背景色。请尝试以下代码:
//发件人:
#import "UIBarButtomItem+Tinted.h"
@implementation UIBarButtonItem (Tinted)
+ (UIBarButtonItem *)newBarButtonItemWithTint:(UIColor*)color andTitle:(NSString*)itemTitle andTarget:(id)theTarget andSelector:(SEL)selector
{
UISegmentedControl *button = [[UISegmentedControl alloc] initWithItems:@[itemTitle]];
button.momentary = YES;
button.segmentedControlStyle = UISegmentedControlStyleBar;
button.tintColor = color;
[button addTarget:theTarget action:selector forControlEvents:UIControlEventValueChanged];
return [[UIBarButtonItem alloc] initWithCustomView:button];
}
@end
关于editButtonParentView,请确保此视图没有UserInteractionEnabled?您希望确保uibarbuttonite响应事件。这只是一个猜测,没有任何效果。谢谢,这会成功的。然而,我仍然很好奇为什么我的方法在我的原始代码中没有被调用。你必须问苹果!我复制了你所拥有的-我也无法获得行动信息。我之前看到的与上面的代码类似-您可以添加一个按钮作为customView,然后像我一样设置按钮的目标/选择器,以使barButton响应。我不知道如何以普通的视角去做。