2015-01-09 69 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類別的標籤代碼。 –