2015-09-16 78 views
1

我有一個UILabel在我已經連接到屬性的界面生成器,但它通過視圖控制器上的viewDidLoad保持零。我一直在介紹,只要DetailViewController初始化,標籤屬性就在那裏,但它是零,它似乎從未被初始化。UILabel永遠不會初始化viewDidLoad

它一直在工作,直到我從導航控制器上使用segues切換到執行pushViewController。

// DetailsViewController.h 

#import <UIKit/UIKit.h> 
#import "TLitem.h" 

@interface DetailsViewController : UIViewController 

@property (nonatomic, strong) TLitem *entry; 
@property (weak, nonatomic) IBOutlet UILabel *entryLabel; 

@end 

然後在另一視圖控制器表格視圖:

// EntryListViewController.m 

DetailsViewController *details = [[DetailsViewController alloc] init]; 
[details setEntry:entry]; 
[self.navigationController pushViewController:details animated:YES]; 

而在viewDidLoad中:

// DetailsViewController.m 
- (void)viewDidLoad { 
    [super viewDidLoad]; 
    [self.view setBackgroundColor:[UIColor yellowColor]]; 
    TLitem *entry = [self entry]; 
    UILabel *label = [self entryLabel]; 

    label.text = [entry valueForKey:@"text"]; 
+0

你提到你塞格斯改爲編程推你的視圖控制器。向我們展示您實例化它的代碼。 –

+0

如果您的視圖控制器視圖和問題標籤是在故事板中定義的,則應該將其實例化爲'UIStoryboard'實例。 –

+0

實例化視圖控制器?這是上面NewEntryListViewController.m的第一行。 – thisAnneM

回答

4

[[DetailsViewController alloc] init]沒有加載從故事板視圖控制器。它會創建一個新的DetailsViewController,但不會連接IB。考慮-instantiateViewControllerWithIdentifier:

它看起來像

DetailsViewController *details = [self.storyboard instantiateViewControllerWithIdentifier:@"Some Storyboard Identifier You Create"]; 
details.entry = entry; 
[self.navigationController pushViewController:details animated:YES]; 
+0

這個。有關插座連接等的所有信息都在故事板中。基於Storyboard的視圖控制器使用'-initWithCoder:'實例化。 –

+0

謝謝! self.storyboard爲零,並表示它是隻讀屬性。有什麼地方可以爲整個應用程序設置故事板嗎? (編輯 - 按下輸入太早)。 – thisAnneM

+0

@thisAnneM編號每個視圖控制器必須使用其創建者(或'storyboardWithName:')的故事板創建。你如何實例化'NewEntryListViewController'? –