我用的是
Kal calendar
. 对于下面显示的代码,我指的是假日示例。在这个例子中,Kal的分配和初始化是在
applicationDidFinishLaunching
AppDelegate
. 这个
UITableViewDelegate
协议(例如。
didSelectRowAtIndexPath
)也位于
应用程序委派
应用程序委派
#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.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
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];
}
@end
我不想把这个放进
应用程序委派
,因为可能存在与其他视图重叠的代码。它应该是一个单独的“组件”,我可以调用它并将其放在堆栈上。
RootViewController
. 从那里我想把Kal视图推到堆栈上。目前我正在推动一个额外的
ViewController
在堆栈上。在
viewWillAppear
-
导航返回必须完成两次(一次用于Kal日历,一次用于我创建的视图)
-
现在我不知道该把代码放在哪里。所以问题是在哪里放置分配/初始化的方法以及
协议。
解决方案:
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];