2015-09-14 6 views
0

Ниже приведен код, где я получаю эту ошибку.NSInvalidArgumentException ', reason:' - [UILabel view]: непризнанный селектор, отправленный в экземпляр 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]]; 

ToolBar, элемент щелкнул событие

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

есть. если элемент barbutton один присутствует, он отлично работает. Но мне также нужно отобразить текст вместе с пунктами баркарт. – jeff

+0

Вы меняете цвет текста вашего ярлыка, потому что текущий textColor белый, поэтому не отображается. @vig –

+0

я удалил эту строку. Но все же я получаю ошибку. – jeff