gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 03:58:06 24 4
gpt4 key购买 nike

我是 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com