0

I'm new to iOS programming, and I'm using the ECSlidingViewController to create a slide out menu (like Facebook). So imagine if I have two views referenced in my menu.

When I open the app, it will obviously call viewDidLoad for my top view (the first one in my menu). If I open the menu and select the second view, it will call viewDidLoad for that too. However, if I go back to the first view, it will call that method again, which I don't want. I have some setup code and I don't want to be reinstantiating views if possible. I've seen Facebook and they don't reinstatiate views, because it remembers my scrolling position on my Wall, for example, when after I switch views and go back.

This is my delegate method that triggers upon selection:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Get identifier from selected
    NSString *identifier = [NSString stringWithFormat:@"%@", [self.menu objectAtIndex:indexPath.row]];

    // Add the selected view to the top view
    UIViewController *newTopVC = [self.storyboard instantiateViewControllerWithIdentifier:identifier];

    // Present it 
    [self.slidingViewController anchorTopViewOffScreenTo:ECRight animations:nil onComplete:^{
        CGRect frame = self.slidingViewController.topViewController.view.frame;
        self.slidingViewController.topViewController = newTopVC;
        self.slidingViewController.topViewController.view.frame = frame;
        [self.slidingViewController resetTopView];
    }];
} 

Is there a way to somehow get a certain VC if it's already been created? That way, it will only call viewWillAppear, and not viewDidLoad more than once.

Thank you.

swiftcode
  • 3,039
  • 9
  • 39
  • 64
  • 2
    Yes, create a strong property for it when you first create it, then use that property rather than instantiating a new one the next time you need it. – rdelmar Apr 26 '13 at 18:45
  • @rdelmar So would that be an array of `UIViewController`s, because I have multiple views? – swiftcode Apr 26 '13 at 18:48

1 Answers1

1

Can Use a navigationcontroller. When you want to go to second view, you can push it the viewcontroller onto the navigation view controller and when you go back, you can pop it off the navigation controller.

EDIT:

If you have 3 views, you can still use navigationcontroller. Same logic as above. But remember to remove the double instances of the same viewcontroller in the navigationcontroller. Look at this page: How to remove a specific view controller from uinavigationcontroller stack?. Check whether the particular viewcontroller exist, if so, remove and then push it on top.

Community
  • 1
  • 1
lakshmen
  • 28,346
  • 66
  • 178
  • 276
  • But what if I have more than 2 views? I don't know if this will work. For example, if I have 3 views. I load 1, then go to 3, then want to go to 2. After that, I want to go to 1 again. How will that work out? – swiftcode Apr 26 '13 at 19:05
  • Is this NavigationController created in code as some sort of stack? I know there's a UINavigationController, but my logic is to really only save and load view states, not segues and all that stuff because my menu implements the navigating ability. – swiftcode Apr 26 '13 at 19:39
  • Oh, and if it helps, I'm using storyboards with ios 6.0 to do this. I basically have an InitViewController, a MenuViewController (which is a UIVC with a UITableView in it w/UITableViewCell), and then 2 UIViewControllers. They are not linked with a Navigation Controller, I simply instantiate each one of those last 2 when the respective VC is selected in the menu. – swiftcode Apr 26 '13 at 19:47