【问题标题】:UIBarButtonItem Target Method is not workingUIBarButtonItem 目标方法不起作用
【发布时间】:2016-02-26 07:44:53
【问题描述】:

实际上我想将图像和标签作为 UIBarButtonItem 放置在工具栏上,并为该按钮提供可点击的效果。

所以我在这里所做的是我创建了一个自定义视图并将 Image 和 Label 放在同一个自定义视图上,最后将此自定义视图放置为 UIBarButtonItem CustomView。

但是当我为同一个 UIBarButtonItem 设置目标和动作时,它并没有调用选择器方法。

整个代码如下。

谁能建议我的代码有什么错误?还有其他方法可以达到同样的效果吗????

非常感谢早期的建议。

- (void)viewDidLoad

{

[super viewDidLoad];
[self.navigationController setToolbarHidden:NO];


 UIView *customView = [[UIView alloc]
initWithFrame:CGRectMake(0,0,self.navigationController.toolbar.frame.size.width,self.navigationController.toolbar.frame.size.height)];

customView.backgroundColor = [UIColor colorWithRed:62.0/255.0 green:187.0/255.0 blue:150.0/255.0 alpha:1.0];

[self.navigationController.toolbar addSubview:customView];


UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(60,0,44,44)];
imgView.image = [UIImage imageNamed:@"documentImage.png"];
[customView addSubview:imgView];


   UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(104,0,145,44)];

   lbl.text = @"Scan Document";

   lbl.textAlignment = NSTextAlignmentLeft;

    [customView addSubview:lbl];

UIBarButtonItem *bar = [[UIBarButtonItem alloc]initWithCustomView:customView];
bar.target = self;
bar.action = @selector(scanDocument);
self.toolbarItems = [NSArray arrayWithObjects:bar, nil];
 }

【问题讨论】:

  • 看到这个对你有帮助stackoverflow.com/questions/2796438/…
  • 您的 scanDocument 定义为接受参数还是不接受参数? -(void) scanDocument:(UIBarButtonItem*)sender-(void)scanDocument
  • 无参数只是-(void) ScanDocument

标签: ios objective-c iphone


【解决方案1】:

如果你想在右手边创建按钮,这里是代码。我已经实现了它并且工作正常。

  //Button Right side
    UIImage *imgFavRest = [UIImage imageNamed:@"documentImage.png"];
    UIButton *cart = [UIButton buttonWithType:UIButtonTypeCustom];
    [cart setImage:imgFavRest forState:UIControlStateNormal];
    cart.frame = CGRectMake(0, 0, imgFavRest.size.width, imgFavRest.size.height);
    [cart addTarget:self action:@selector(showCart) forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithCustomView:cart];
    self.navigationItem.rightBarButtonItem = rightBtn;

然后使用这样的方法。就是这样

-(void)cartItemCount{
// Method
}

【讨论】:

  • 图片应该显示在标签之前。所以,它应该像 Image first Label Next。我想在调用选择器方法之前对两者都应用目标操作并提供可点击的效果。
  • 谢谢@Cade。它工作正常,但我想总是在视图控制器的底部显示这个按钮,总是在顶部(意味着我在视图控制器上有一个滚动视图,我想总是在滚动视图的顶部显示这个按钮)。
【解决方案2】:
    float textwidth = 50; // according to your text
    UIImage *image = [UIImage imageNamed:@"logout"];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.bounds = CGRectMake( 10, 0, image.size.width+textwidth, image.size.height );
    [btn addTarget:self action:@selector(scanDocument) forControlEvents:UIControlEventTouchUpInside];
    [btn setImage:image forState:UIControlStateNormal];
    [btn setTitle:@"test" forState:UIControlStateNormal];
    [btn setTitleEdgeInsets:UIEdgeInsetsMake(0.0, 5.0, 0.0, 0.0)];

    //Adjust the coordinates and size of your button as your requirement.This is a sample code to guide you.
    //PS:Take a UIButton in the nib file>Make it a custom button>Connect the IBOutlet to your custom button in the nib file.Thats it.

    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.navigationItem.rightBarButtonItem = rightButton;

我看到非常复杂的答案,所有答案都使用代码。但是,如果您使用的是 Interface Builder,有一种非常简单的方法可以做到这一点:

  • 选择按钮并设置标题和图像。请注意,如果您设置 背景而不是图像然后图像将被调整大小如果 它比按钮小。IB基本图像和标题
  • 通过更改边缘和插图来设置这两个项目的位置。你 甚至可以在控制部分控制两者的对齐方式。
  • IB 位置设置 IB 控制设置

您甚至可以通过代码使用相同的方法,而无需像其他建议的解决方案那样在内部创建 UILabel 和 UIImage。始终保持简单!

编辑:附上一个小示例,其中设置了 3 个内容(标题、图像和背景),并带有正确的插图按钮预览

注意:: UIBarButtonItem 继承自 UIBarItemNSObject 所以它对触摸一无所知。如果文档提到 action 和 target 属性仅在自定义视图是 UIButton 时适用,那就太好了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-09
    • 2012-07-07
    • 1970-01-01
    相关资源
    最近更新 更多