Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UIBarButtonItem目标方法无效_Ios_Objective C_Iphone - Fatal编程技术网

Ios UIBarButtonItem目标方法无效

Ios UIBarButtonItem目标方法无效,ios,objective-c,iphone,Ios,Objective C,Iphone,实际上,我想将图像和标签作为UIBarButtonim放置在工具栏上,并为该按钮提供可单击的效果 我在这里做的是创建了一个自定义视图,将图像和标签放在同一个自定义视图上,最后将这个自定义视图作为UIBarButtonItem自定义视图放置 但当我为同一个uibarbuttonite设置目标和操作时,它并没有调用选择器方法 整个代码如下 有人能告诉我我的密码有什么错误吗?还有其他方法可以达到同样的效果吗 如能早日提出建议,将不胜感激 - (void)viewDidLoad { [super v

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

我在这里做的是创建了一个自定义视图,将图像和标签放在同一个自定义视图上,最后将这个自定义视图作为UIBarButtonItem自定义视图放置

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

整个代码如下

有人能告诉我我的密码有什么错误吗?还有其他方法可以达到同样的效果吗

如能早日提出建议,将不胜感激

- (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];
 }

如果您想创建按钮右侧,这里是代码。我已经实现了它,并且工作得很好

  //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
}
我看到非常复杂的答案,所有答案都使用代码。但是,如果您使用的是Interface Builder,则有一种非常简单的方法:

  • 选择按钮并设置标题和图像。请注意,如果设置 如果出现以下情况,则将调整背景而不是图像的大小: 它比按钮小。IB基本图像和标题
  • 通过更改边和插入来设置两个项目的位置。你 甚至可以在控制部分控制两者的对齐。
  • IB位置集IB控制集
您甚至可以通过代码使用相同的方法,而无需像其他建议的解决方案那样在内部创建UILabel和UIImages。永远保持简单

编辑:附上一个小例子,有3件事设置(标题,图像和背景)与正确的插入按钮预览


注意::
UIBarButtonItem
继承自
UIBarItem
NSObject
,因此它对触摸一无所知。如果文档提到只有在自定义视图为
ui按钮时,操作和目标属性才适用,那就太好了。看到了吗?您的扫描文档定义为接受参数还是不接受参数
-(void)ScandDocument:(UIBarButtonItem*)发送方
-(void)ScandDocument
任何参数都不应在标签前显示-(void)ScandDocument图像。所以,它应该像图像第一标签下。我想在调用selector方法之前,将目标动作应用于这两种方法,并提供可点击的效果。谢谢@Cade。它工作正常,但我希望始终在viewcontroller底部显示此按钮始终在顶部(意味着我在viewcontroller上有一个scrollview,我希望始终在scrollview顶部显示此按钮)。
    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;