1

I'm using the ECSlidingViewController to create a slide out menu (like Facebook).

I have this storyboard:

Storyboard

As you can see, I have a navigation controller. I have a major problem though, that even the official demo created by the user who made that controller didn't implement: it doesn't save the state of the view controller when changing views and then coming back.

So, for example, the orange view will always be the first view when I open the app, it gets viewDidLoad. Then I switch to my green view (the second one), and click the button. It changes the background color of that view to red. Then if I go back to my first view, and then back to the second one, the background color of the latter is green again. I want it to stay red.

This is my code to switch views (in MenuViewController):

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

    }];
}

As you can see, it's always instantiating a new VC every time. I want it to save the VC, create the new one if it's not created, then show that one. If the user goes back to a view that has already been created, it should just restore the saved view, not create a new one.

I have put the Init View Controller in a Navigation Controller, now how can I implement this save/restore mechanism for my views? I'd like it to work with 2,3,4, etc....as many views as possible.

Thanks.

4

1 回答 1

5

当您“返回”时,您实际上是从导航堆栈中弹出视图控制器。那时,不再有对该视图控制器的引用,并且它被释放,因此您丢失了所有更改。

您可以通过以下几种方式处理:

1)在父视图控制器(正在呈现的视图控制器)中保持对红色/绿色视图控制器的引用并使用它而不是实例化一个新视图控制器。这对内存不是很友好,但如果使用得少,可以使用。

在界面中输入:

@property (nonatomic, strong) UIViewController* myGreenController;

然后将实例化更改为

if (!self.myGreenController)
{
   self.myGreenController = [self.storyboard instantiateViewControllerWithIdentifier:identifier];
}
...
self.slidingViewController.topViewController = self.myGreenController;

2)理想情况下,实现一个委托模式将状态传递回父视图控制器(类似于我如何设置一个简单的委托来在两个视图控制器之间进行通信?)。然后下次当您需要 viewController 时,您可以在呈现之前设置状态。

于 2013-04-26T23:25:20.343 回答