使用 Kal 日历而不在 AppDelegate 中进行初始化(等等)

发布于 2024-09-24 07:37:38 字数 2736 浏览 2 评论 0原文

我正在使用 Kal 日历。对于下面显示的代码,我指的是假日示例。在此示例中,Kal 的分配和初始化是在 AppDelegate 中的 applicationDidFinishLaunching 中完成的。 UITableViewDelegate 协议(例如didSelectRowAtIndexPath)也位于AppDelegate 类中。

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.
- (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

我不想将其放入 AppDelegate 中,因为可能存在与其他视图重叠的代码。它应该是一个单独的“组件”,我可以调用它并将其放入堆栈中。

在我的基于导航的项目中,我有一个主视图,RootViewController。从那里我想将 Kal 视图推送到堆栈上。目前我正在堆栈上推送一个额外的 ViewController 。在此 ViewControllerviewWillAppear 方法中,我执行了上面代码中所示的操作。出现以下问题:

  • 导航返回必须完成两次(一次用于 Kal 日历,一次用于我创建的视图)
  • 导航到我的主视图不再可能

现在我不知道在哪里放置此代码。所以问题是在哪里放置分配/初始化方法以及 UITableViewDelegate 协议的方法。

解决方案:

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];

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.
- (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

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.

Solution:

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];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北陌 2024-10-01 07:37:38

我已经在 RootViewController 中完成了初始化。这不是最好的解决方案,但我没有找到另一种解决方案。

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

夏雨凉 2024-10-01 07:37:38

建议阅读 Apple 的 SimpleEKDemo 示例。

Suggest to read the SimpleEKDemo example from Apple.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文