我正在将我的应用程序转移到 Storyboards,到目前为止一切顺利。
但是,我发现了一些我并不真正理解并让我担心的事情。如果有人可以对此提供一些见解,我将不胜感激。
我的应用程序使用普通的导航 Controller 。为了“前进”到新的 View Controller ,我使用了自定义转场;那里没有问题。但是,应用程序中有一点我想移回到导航堆栈的开头。我还使用自定义 segue 配置了“导航”,为此,我通过将最后一个 View Controller 拖到第一个 View Controller (对我来说已经很奇怪)在 Interface Builder 中创建了 segue,并且我已经实现了自定义 segue按以下方式执行方法:
-(void)perform
{
UIViewController *src = (UIViewController *)self.sourceViewController;
UIViewController *dest = (UIViewController *)self.destinationViewController;
[src.navigationController popToRootViewControllerAnimated:NO];
// Custom animation code here
}
... 效果很好。但是,我不明白为什么会这样。在我看来,自定义 segue 应该实例化我的第一个 View Controller 的一个新实例并将其分配为“dest”,但看起来 segue 足够聪明,可以意识到我想导航到一个先前存在的实例查看 Controller ,而不是创建一个新实例,它分配给“dest”现有实例。
有人知道以这种方式使用 segues 是否可以吗?它是否有可能偶然起作用但将来可能会停止工作?无论如何,我是否在浪费内存,因为 segue 正在实例化一个我不打算使用的 View Controller ?
提前致谢!
最佳答案
-
Am I wasting memory in anyway as the segue is instantiating a View Controller I'm not going to use?
DestinationController
属性。自己测试:在你的根 Controller 中添加一个静态计数器,每次初始化这个类时递增它并在你的 View 中显示它:你会看到它在每次你使用这个技巧弹出到根 Controller 时递增。
-
Does anybody know if using segues in this way is ok?
这个问题至少有一个解决方案:在您的(void)perform
实现中释放转场的DestinationController
。这实现起来真的很快,但是有点难看,因为你每次都分配并立即释放你的 View Controller ......即使它比仅仅泄漏它更好,这不是我所说的好习惯!
在我看来,实现您想要的效果的更好方法是不为该转换使用 segue,只需使用按钮或其他任何东西并在触摸此按钮时调用 popToRootViewController:animated
。
-
Is it possible that it works by chance but might stop working in the future?
关于ios - 使用自定义 segue 在导航堆栈上向后移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10444371/