ios - 如何获取已经实例化的UIViewController?

标签 ios objective-c uiviewcontroller storyboard

我是 iOS 编程新手,我正在使用 ECSlidingViewController创建滑出菜单(如 Facebook)。所以想象一下,如果我的菜单中引用了两个 View 。

当我打开应用程序时,它显然会为我的顶 View (菜单中的第一个 View )调用viewDidLoad。如果我打开菜单并选择第二个 View ,它也会为此调用 viewDidLoad 。但是,如果我返回到第一个 View ,它将再次调用该方法,这是我不希望的。我有一些设置代码,如果可能的话,我不想重新实例化 View 。我见过 Facebook,他们不会恢复 View ,因为它会记住我在墙上的滚动位置,例如,当我切换 View 并返回时。

这是我的委托(delegate)方法,在选择时触发:

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

如果已经创建了某个 VC,有没有办法以某种方式获得它?这样,它只会调用 viewWillAppear,而不会多次调用 viewDidLoad

谢谢。

最佳答案

可以使用导航 Controller 。当你想进入第二个 View 时,你可以将 View Controller 推到导航 View Controller 上,当你返回时,你可以将其从导航 Controller 上弹出。

编辑:

如果您有 3 个 View ,您仍然可以使用导航 Controller 。与上面的逻辑相同。但请记住删除导航 Controller 中同一 View Controller 的双重实例。看这个页面:How to remove a specific view controller from uinavigationcontroller stack? .检查特定的 View Controller 是否存在,如果存在,则删除然后将其推到顶部。

关于ios - 如何获取已经实例化的UIViewController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16243074/

相关文章:

ios - 如何维护用户已经在 ios 应用程序中登录的 session

ios - 自定义 MKAnnotationView 间歇性识别触摸

ios - 如何使用 Swift 使用 TouchID?

iphone - 推送 View 而不是从基于单一 View 的应用程序推送 UIViewController

ios - UILabel 创建后始终为 nil

ios - 快速将 UITableView 编辑按钮添加到工具栏

ios - 无法使用自动布局和 Storyboard将 tableHeaderView 中的 View 居中

ios - 使用 UIControlEventTouchDragEnter 触发按钮的方法......但不起作用?

iphone - UITableView deleteRowsAtIndexPaths 不删除行

ios - 将 IBDesignable 和 IBInspectable 与 UIViewController 一起使用