ios - 如何在不重新加载主屏幕的情况下创建滑出式菜单(如 Facebook 应用程序)? (iOS)

标签 ios uitableview swrevealviewcontroller tabslideout

所以我一直在使用this tutorial在我的 iOS 应用程序上实现滑动菜单。但是,当我尝试按下主单元格按钮时,主 Controller 会刷新,因为据我所知,它将另一个版本的 Controller 放在旧 Controller 之上。我正在构建一个游戏,以便您了解为什么这种情况不理想——本质上,每次用户使用滑出菜单时它都会创建一个新游戏。无论如何,有没有办法防止这种刷新发生或有其他可能的解决方法?

最佳答案

根据教程告诉您的内容,将对菜单中的每个单元格项目执行转场。为了防止重新加载已经存在的 View Controller ,您应该做的是在 didSelectRowAtIndexPath: 中执行 segue,除了检查当前 View Controller 是否等于您选择的 View Controller 。您应该使菜单 Controller 的所有可能的 View Controller 属性成为菜单 Controller 的根 Controller 。这样你就可以检测最后一个 View Controller 是否是被选中的。在委托(delegate)方法 didSelectRowAtIndexPath: 中管理菜单的 Controller 上,添加:

在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:所以基本上,实例化 Controller 一次:

然后将您的对象实例化放置在 oneViewController.m 中的适当位置。例如,您不希望可变的对象(我假设是家庭 Controller 上的所有对象)应该在这里分配和初始化。这将要求您将大部分 UI 机制转移到编程上,而不是 Storyboard。我相信当您执行转场时,它会分配并创建一个新的 View Controller ,然后运行 ​​performSegueWithIdentifier:animated:

EDIT2:我刚刚意识到我输错了我的代码,它们现在应该包含最新的修订版,对于造成的困惑我深表歉意。

EDIT3:如果您仍想保留方法滑过的方式,我对动画呈现和关闭 View Controller 并不太特别。但是,只需通过 doing the method from this post 严格实例化即可应用相同的逻辑。如果您不想打扰:

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

然后您可以在不分配全新 Controller 的情况下执行 segue。

关于ios - 如何在不重新加载主屏幕的情况下创建滑出式菜单(如 Facebook 应用程序)? (iOS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23553214/

相关文章:

ios - swift 2 TapGestureRecognizer 不接受自己作为目标

ios - 从 View Controller 到自身的 Storyboard Segue

ios - 在多个 View 中使用 SWReveal

iOS:SWRevealViewController 和多个 NavigationControllers

ios - MapKit 注释消失

objective-c - 将按钮(UIButton)添加到使用 drawRect : 绘制的自定义 tableview 单元格

iphone - 滚动时 UITableviewCell 问题

iphone - 如何在 uitextfield 委托(delegate)方法中获取单元格索引路径?

ios - 在 Xcode 中使用 UINavigationController 时如何更改 UIStatusBar 的背景颜色?

iphone - 尝试使用 Copy.asmx Web 服务将内容上传到 Sharepoint 站点时出现 400 Bad Request