2015-09-14 79 views
0

下面是我得到这个错误的代码。NSInvalidArgumentException',原因:' - [UILabel视图]:无法识别的选择器发送到实例0x7ffbd0da2680'

UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, self.view.bounds.size.height - 44.0f, self.view.bounds.size.width, 44.0f)]; 
    toolBar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin; 
    [self.view addSubview:toolBar];  
    UIBarButtonItem *people = [[UIBarButtonItem alloc] initWithTitle:@"people" style:UIBarButtonItemStylePlain target:self action:@selector(tap:)]; 
    UIBarButtonItem *food = [[UIBarButtonItem alloc] initWithTitle:@"food" style:UIBarButtonItemStylePlain target:self action:@selector(tap:)]; 
    UIBarButtonItem *nature = [[UIBarButtonItem alloc] initWithTitle:@"nature" style:UIBarButtonItemStylePlain target:self action:@selector(tap:)]; 
    UIBarButtonItem *sports = [[UIBarButtonItem alloc] initWithTitle:@"sports" style:UIBarButtonItemStylePlain target:self action:@selector(tap:)]; 
    UIBarButtonItem *cats = [[UIBarButtonItem alloc] initWithTitle:@"cats" style:UIBarButtonItemStylePlain target:self action:@selector(tap:)]; 
    UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 
    UILabel *lbl1 = [[UILabel alloc] init]; 
    [lbl1 setFrame:CGRectMake(0,5,100,20)]; 
    lbl1.backgroundColor=[UIColor clearColor]; 
    lbl1.textColor=[UIColor whiteColor]; 
    lbl1.userInteractionEnabled=YES; 
    [self.view addSubview:lbl1]; 
    lbl1.text= @"TEST"; 
    [toolBar setItems:@[space, lbl1, people, food, nature, sports, cats, space]]; 
+1

为什么工具栏上要添加标签? –

+0

我想显示除barbutton项目外的文本。那就是原因... – jeff

+0

将它添加到上面或下面,而不是在工具栏上。 – brandonscript

回答

1

由于您在toolBar中添加了标签,因此您的错误在于此行。使用此代码

UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, self.view.bounds.size.height - 44.0f, self.view.bounds.size.width, 44.0f)]; 
toolBar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin; 
[self.view addSubview:toolBar]; 
UIBarButtonItem *people = [[UIBarButtonItem alloc] initWithTitle:@"people" style:UIBarButtonItemStylePlain target:self action:@selector(tap:)]; 
UIBarButtonItem *food = [[UIBarButtonItem alloc] initWithTitle:@"food" style:UIBarButtonItemStylePlain target:self action:@selector(tap:)]; 
UIBarButtonItem *nature = [[UIBarButtonItem alloc] initWithTitle:@"nature" style:UIBarButtonItemStylePlain target:self action:@selector(tap:)]; 
UIBarButtonItem *sports = [[UIBarButtonItem alloc] initWithTitle:@"sports" style:UIBarButtonItemStylePlain target:self action:@selector(tap:)]; 
UIBarButtonItem *cats = [[UIBarButtonItem alloc] initWithTitle:@"cats" style:UIBarButtonItemStylePlain target:self action:@selector(tap:)]; 
UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 
lbl1 = [[UILabel alloc] init]; 
[lbl1 setFrame:CGRectMake(0,64,100,20)]; 
lbl1.backgroundColor=[UIColor clearColor]; 
lbl1.textColor=[UIColor redColor]; 
lbl1.userInteractionEnabled=YES; 
[self.view addSubview:lbl1]; 
lbl1.text= @"TEST"; 
[toolBar setItems:@[space, people, food, nature, sports, cats, space]]; 

工具栏项目点击事件

-(IBAction)tap:(UIBarButtonItem*)sender{ 
    lbl1.text= sender.title; 
} 
+0

是的。如果单独存在barbutton项目,它可以正常工作。但我也需要显示一个文本以及barbutton项目。 – jeff

+0

由于当前textColor为白色,所以不能显示,因此更改标签文本颜色。 @vig –

+0

我删除了那一行。但我仍然得到错误。 – jeff

相关问题