我检查了 KalViewController 类。它没有initWithCoder:
实现,如果从 nibs/storyboards 实例化则调用的初始化程序。
我添加了这个以使其工作:
-(id)initWithCoder:(NSCoder *)aDecoder
{
if ((self = [super initWithCoder:aDecoder])) {
NSDate *date = [NSDate date];
logic = [[KalLogic alloc] initForDate:date];
self.initialDate = date;
self.selectedDate = date;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(significantTimeChangeOccurred) name:UIApplicationSignificantTimeChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadData) name:KalDataSourceChangedNotification object:nil];
}
return self;
}
比我只是将 UIViewController 拖到 storybord 舞台上,在检查器中将它的类更改为 KalViewController,将它从标签栏控制器连接起来,它就可以工作了。
我创建了一个示例项目:TabbedKalTest@GitHub
当然 DRY 应该记住:
-(void) _configureWithDate:(NSDate *)date
{
logic = [[KalLogic alloc] initForDate:date];
self.initialDate = date;
self.selectedDate = date;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(significantTimeChangeOccurred) name:UIApplicationSignificantTimeChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadData) name:KalDataSourceChangedNotification object:nil];
}
//called if created by nib/storyboard
-(id)initWithCoder:(NSCoder *)aDecoder
{
if ((self = [super initWithCoder:aDecoder])) {
[self _configureWithDate:[NSDate date]];
}
return self;
}
//the designated initializer for non-nib/storyboard creation
- (id)initWithSelectedDate:(NSDate *)date
{
if ((self = [super init])) {
[self _configureWithDate:date];
}
return self;
}
我在 Kal 创建了一个分支来解决这个问题并发布了一个pull-request。