3

关于如何解决这个问题的任何建议,或者更好的实现设计?

要求

  • 需要一种方法让应用程序在启动时将用户带到上一个详细信息页面,如果这是他们在上次会话中退出应用程序之前所做的事情
  • 如果他们在应用程序的主屏幕上,那么在重新启动时他们可以留在这里
  • 假设我正在使用 UINavigationController 并且主屏幕和详细信息屏幕构建在 UITableViewController 上

我的实施理念

  • 检查“viewdidLoad”以查看它们是否在详细屏幕上,如果是,则跳转到此(参考下面的代码)

问题

  • 正常工作,但是当我触发内存警告时,事情就搞砸了,我得到导航栏的奇怪行为。例如,当我看起来像是在详细页面内容(UITableView)上时,我看到了主页导航按钮

我的分析

  • 从我在详细信息页面(appointmentsListController)上看到的并在模拟器中引起内存警告时,我看到:

    (a) 主页“viewDidLoad”实际上被调用了,这是我的概念没有预料到的,所以当我从详细视图 (UINavigationController) 中点击 BACK 按钮转到主视图 (RootViewController) 时,实际上是我的代码正在运行,它会尝试再次将用户返回到详细信息页面

    (b) 在此之后,我在日志中注意到 [AppointmentListController viewDidLoad] 似乎在调用之前的 AppointmentListController dealloc 方法之前被调用(即,就像我在控制器 A 中,回到控制器 B,但又被扔回 A -并且第一部分的初始dealloc直到很晚才开始......)

  • 所以我想很明显我的想法不太好

问题

关于如何更好地实现我的要求的任何建议?(如何检查,放入哪种方法)

代码

- (void)viewDidLoad {
    [super viewDidLoad];

    // My Implementation of the Requirements which seems flawed in the case there is memory warning triggered 
    if ( previousSelectedScreen >= 0 ) {

        // Setup New Controller
        AppointmentListController *appointmentListController = [[AppointmentListController alloc] initWithNibName:@"AppointmentListController" bundle:nil];
        appointmentListController.screenToShow = previousSelectedScreen;

        // Push new view onto stack
        [[self navigationController] pushViewController:appointmentListController animated:NO];
        [appointmentListController release]; 
    } 

}
4

1 回答 1

2

Here's what I'd suggest: rather than having this logic in your view controller, but it in your application delegate. By constructing your navigation stack before displaying it you will hopefully avoid some of the weird things that can happen with nav bars, etc. To get rid of the memory warnings you may need to look at how your app allocates memory: it may not necessarily be to do with this.

Anyway - in your application delegate you can perform your check to see if the user was on a detail page when they exited. If they are, you can create an array containing the navigation stack (ie, Main Screen -> Details Page). You can then pass this into a navigation controller using its setViewControllers method. Once this is done, you can display your window and finish launching the app.

于 2011-06-05T10:39:15.007 回答