UILabel永远不会在viewDidLoad中初始化

时间:2015-09-16 02:11:12

标签: ios objective-c uilabel

我在界面构建器中有一个UILabel,我已经连接到一个属性,但它在该视图控制器上通过viewDidLoad保持为零。我一直在单步执行,一旦DetailViewController被初始化,label属性就在那里,但它是nil,它似乎永远不会被初始化。

直到我从使用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"];

1 个答案:

答案 0 :(得分: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];