Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I implement an automatic jump to a detailed page if the user was on this previously (or fix my code for doing this which has a design flaw)

Any advice on how to fix this issue I have, or a better implementation design perhaps?

Requirement

  • Needed a way for the application at start up to take the user to the previous details page, if this was what they were on prior to quiting the application in their last session
  • If they were on the main screen of the app, then at restart they can stay here
  • The assumption is I'm working with UINavigationController and the main screen and details screen are built on a UITableViewController

My Implementation Concept

  • Put a check in "viewdidLoad" to see whether they were on a detailed screen, and then if so jump to this (refer to code below)

Issue

  • Works fine normally, however when I trigger a memory warning things screw up and I get nav bar weird behavior. For example I see the main page nav buttons, when it looks like I'm on the detail page content (UITableView)

My Analysis

  • From what I see when I am on the details page (appointmentsListController) and cause a memory warning in the simulator I see:

    (a) the main page "viewDidLoad" actually gets called, which my concept didn't expect, so whilst I had hit the BACK button from the detailed view (UINavigationController) to go to the main view (RootViewController), in fact my code is run and it try's to throw the user back to the details page again

    (b) I note in my logs after this point that [AppointmentListController viewDidLoad] seems to get called before the previous AppointmentListController dealloc method is called (i.e. like I was in controller A, went back to controller B, but got thrown back to A - and the initial dealloc for the first part didn't kick in until late...)

  • So I guess it's obvious my idea isn't too great

Question

Any suggestions on how to better implement my requirement? (how to check, which method to put them in)

Code

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

}
like image 440
Greg Avatar asked Dec 20 '25 21:12

Greg


1 Answers

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.

like image 195
lxt Avatar answered Dec 22 '25 11:12

lxt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!