Take the 2-minute tour ×

I'm using the Kal calendar. For the code shown below I'm referring to the Holiday example. In this example the allocation and initialization of Kal is done in the applicationDidFinishLaunching in the AppDelegate. The UITableViewDelegate protocol (e.g. didSelectRowAtIndexPath) is also positioned in the AppDelegate class.

The AppDelegate:

#import "HolidayAppDelegate.h"
#import "HolidaySqliteDataSource.h"
#import "HolidaysDetailViewController.h"
## Heading ###import "Kal.h"

@implementation HolidayAppDelegate

@synthesize window;

- (void)applicationDidFinishLaunching:(UIApplication *)application
  kal = [[KalViewController alloc] init];

  kal.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Today" style:UIBarButtonItemStyleBordered target:self action:@selector(showAndSelectToday)] autorelease];
  kal.delegate = self;
  dataSource = [[HolidaySqliteDataSource alloc] init];
  kal.dataSource = dataSource;

  // Setup the navigation stack and display it.
  navController = [[UINavigationController alloc] initWithRootViewController:kal];
  [window addSubview:navController.view];
  [window makeKeyAndVisible];

// Action handler for the navigation bar's right bar button item.
- (void)showAndSelectToday
  [kal showAndSelectDate:[NSDate date]];

#pragma mark UITableViewDelegate protocol conformance

// Display a details screen for the selected holiday/row.
  Holiday *holiday = [dataSource holidayAtIndexPath:indexPath];
  HolidaysDetailViewController *vc = [[[HolidaysDetailViewController alloc] initWithHoliday:holiday] autorelease];
  [navController pushViewController:vc animated:YES];

#pragma mark -

- (void)dealloc
  [kal release];
  [dataSource release];
  [window release];
  [navController release];
  [super dealloc];


I don't want to put this into the AppDelegate, because there could be some overlapping code with other views. It should be a separate "component" which I can call and put on the stack.

In my navigation based project I have a main view, the RootViewController. From there I want to push the Kal view on the stack. Currently I'm pushing an additional ViewController on the stack. In the viewWillAppear method from this ViewController I do the things shown in the code above. The following problems appear:

  • Navigation back has to be done two times (one for the Kal calendar, one for my created view)
  • Navigation to my main view is not possible anymore

In the moment I don't know where to put this code. So the question is where to put the methods for allocation/initialization as well as the methods for the UITableViewDelegate protocol.


if (kal == nil) {
    kal = [[KalViewController alloc] init];
    kal.navigationItem.title = NSLocalizedString(@"Timetable",@"");
    kal.delegate = self;
    self.dataSource = [[[MyDataSource alloc] init] autorelease];
    kal.dataSource = dataSource;
[[self navigationController] pushViewController:kal animated:YES];
share|improve this question

2 Answers 2

up vote 0 down vote accepted

I've done the initialization in my RootViewController. This isn't the best solution, but I didn't found another one.

share|improve this answer

Suggest to read the SimpleEKDemo example from Apple.

share|improve this answer
What is the bottom line? –  testing Dec 26 '10 at 18:21
It should be ported to other controller other than AppDelegate by following the logic of the example. For iPhone application, porting to RootViewController is good. For iPad application with UISplitViewController, it could be ported to either masterViewController or DetailViewController. Cheer. –  RoIT Dec 27 '10 at 15:21

Your Answer


