0

kalカレンダーフレームワークを使用したい。しかし、これをタブバーアプリケーションに実装する方法がわかりません。kalViewControllerを初期化する方法のデモを見ると、次のようになっています。

-(void)viewDidLoad{
    KalViewController *calendar = [[KalViewController alloc] init];
    [self.navigationController pushViewController:calendar animated:YES];
}

これは機能しますが、別のビューに移動します。そして、ストーリーボードを使用して、同じビューで表示したいと思います。

4

1 に答える 1

2

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でこれを修正するためのブランチを作成し、プルリクエストを投稿しました。

于 2012-12-19T14:08:46.033 に答える