0

所以我一直在使用本教程在我的 iOS 应用程序上实现滑动菜单。但是,当我尝试点击主页单元按钮时,主页控制器会刷新,因为据我了解,它将另一个版本的控制器放在旧版本之上。我正在构建一个游戏,以便您了解为什么这种情况并不理想——本质上,它是在用户每次使用滑出菜单时创建一个新游戏。有没有办法防止这种刷新发生或替代可能的解决方法?

4

1 回答 1

1

根据教程告诉您的内容,菜单中的每个单元格项目都会执行一个 segue。为了防止重新加载已经存在的视图控制器,您应该做的是执行 segue in didSelectRowAtIndexPath:,除了检查当前视图控制器是否等于您选择的视图控制器。您应该使菜单控制器的所有可能的视图控制器属性,并将菜单控制器作为根控制器。通过这种方式,您可以检测最后一个视图控制器是否是选定的。在委托方法中管理菜单的控制器上didSelectRowAtIndexPath:,添加以下内容:

在 menuController.h

@property (retain, nonatomic) OneViewController *thatViewController;

在 menuController.m 的 didSelectRowForIndexPath 中:

NSArray *segueIdentifiers = @[@"firstSegueID",@"secondSegueID",...];//list of all segues possible in order matching the rows, so row0 would associate to the firstSegueID
if ([segueIdentifiers objectAtIndex:indexPath.row] isEqualToString:@"firstSegueID"])
{
    BOOL isInstantiated;
    isInstantiated = NO;
    if (self.thatViewController == nil) //or some way to check if it was already instantiated like testing a property of the controller
    {
        self.thatViewController = [[OneViewController alloc] init]; //or whatever init class method you like
        [self.thatViewController.property setThemHere];

        isInstantiated = YES;
    }

    [self presentViewController:self.thatViewController animated:YES completion:^{NSLog(@"presented that view controller, was reloaded: %@",isInstantiated);}];
}

EDIT1:所以基本上,将控制器实例化一次:

然后将您的对象实例化放在 oneViewController.m 中的适当位置。例如,您不希望易失的对象(我假设是家庭控制器上的所有内容)应该在此处分配和初始化。这将要求您将大部分 UI 机制转移到编程并远离故事板。我相信当你执行 segue 时,它​​会分配并创建一个新的视图控制器,然后运行performSegueWithIdentifier:animated:​​.

EDIT2:我刚刚意识到我打错了我的代码,它们现在应该包含最新版本,对于造成的混乱,我深表歉意。

EDIT3:如果您仍然想保留方法滑过的方式,我对动画呈现和关闭视图控制器并不太特别。但是,可以通过执行本文中的方法严格地实例化它来应用相同的逻辑。如果你不想打扰:

if (! self.thatViewController) {
    self.thatViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"HomeViewController"]; // set this property in the story board
}

然后你可以在不分配全新控制器的情况下执行 segue。

于 2014-05-09T22:12:58.143 回答