2015-01-09 71 views
1

我有一个viewController类别和在类别中有一个UILabel。 我想从另一个viewController设置标签标题。从UIViewController设置标签标题

当我试图崩溃。

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[BaseViewController setTitleLabel:]: unrecognized selector sent to instance 0x7fcfe857a580' 

我该如何做到这一点。

编辑:

TitleLabel是A类标签,这里是UIViewController中类代码

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; 
    btn.frame = CGRectMake(0, 0, 50, 50); 
    [btn setImage:[UIImage imageNamed:@"Menu"] forState:UIControlStateNormal]; 
    [btn addTarget:self action:@selector(MenuPressed:) forControlEvents:UIControlEventTouchUpInside]; 
    UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc]initWithCustomView:btn]; 

    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom]; 
    btn1.frame = CGRectMake(0, 0, 70, 44); 
    [btn1 setTitle:@"Clickme!" forState:UIControlStateNormal]; 
    btn1.backgroundColor = [UIColor blueColor]; 


    UILabel *titleLabel = [UILabel new]; 
    titleLabel.frame = CGRectMake(0, 0, 100, 44); 
    titleLabel.backgroundColor =[UIColor cyanColor]; 

    UIBarButtonItem *barBtn2 = [[UIBarButtonItem alloc]initWithCustomView:btn1]; 
    UIBarButtonItem *barTitle = [[UIBarButtonItem alloc]initWithCustomView:titleLabel]; 
    UIBarButtonItem *flexibleItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 

    NSArray *barBtnArray = @[leftBtn,flexibleItem,barTitle,barBtn2]; 
    self.navigationItem.leftBarButtonItems = barBtnArray; 

回答

0

你不能这样做,因为标签属性在.m文件中声明。它不会在外面访问,如果你尝试访问,那么它会崩溃(这是私人财产)。你有2个选择来解决这个问题。

选项1

.h文件中声明标签属性本身

选项2

写这需要一个NSString因为它的参数,并将其设置的值的公共方法你标签。

0

UIViewController不具有titleLabel的属性。检查你的类别。

+0

titleLabel是类别中的标签检查我的编辑; –

+0

你可以发布一些更多的代码,所以我们可以找出问题。 – abhishekkharwar

+0

我已经添加了UIViewcontroller类别的标签代码。 –