故事板会导致内存泄漏 [英] Storyboard segues causing memory leaks

查看:150
本文介绍了故事板会导致内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个UIViewControllers与按钮触发segue(modal)彼此。我想发现,如果这是造成任何内存泄漏,而来回跳动,我看到生活对象&分配的内存正在上升,最终会留给应用程序崩溃。我没有任何单行代码 - 使用纯UIViewControllers。

I have two UIViewControllers with buttons triggering segue (modal) to each other. I wanted to discover if that's causing any memory leaks while jumping back and forth and I see that Living Object && allocated memory is going up, what eventually would leave to app crash. I don't have any single line of code - working with pure UIViewControllers.


我可能做错什么?
我可能在项目设置中设置了错误吗?
我是否严重地读取profiler的静态绑定?
在使用segue时,我需要执行任何特殊的释放命令吗?

What I might be doing wrong?
Could have I set something wrong in project settings?
Am I reading profiler's statictics badly?
Do I need to make any special release commands while working with segues?

推荐答案

您没有正确使用模态段落。你实现它的方式,你创建一个新的实例的每个视图控制器,当你segue,而不是返回到你的实例。这是为什么你的内存使用率持续增加。

You aren't using the modal segues correctly. The way you have implemented it, you are creating a new instance of each view controller when you segue instead of returning to the instance you came from. That is why your memory usage continues to increase.

在iOS 6之前,正确的处理方式是:

Before iOS 6, the correct way to handle this was to:

1)在视图控制器1中定义一个诸如 viewController2Done 的方法

2)在视图控制器2中,创建一个名为 $ 中的 c>委托 c> >对于视图控制器1,将视图控制器2中的委托设置为 self

4)视图控制器2,当是时候返回到视图控制器1,调用 [delegate viewController2Done]

5)在 viewController2Done 调用 [self dismissModalViewControllerAnimated:YES]

1) define a method such as viewController2Done in view controller 1
2) in view controller 2, create a property called delegate of type id.
3) in prepareToSegue for view controller 1, set delegate in view controller 2 to self
4) in view controller 2, when it is time to return to view controller 1, call [delegate viewController2Done]
5) in viewController2Done call [self dismissModalViewControllerAnimated:YES]

iOS 6,但也有一个新的 unwind segue 可以使用。要使用它,您需要在视图控制器1中定义一个方法,如下所示:

This method still works in iOS 6, but there is also a new unwind segue that can be used instead. To use it, you would define a method in your view controller 1 like so:

Objective-C:

- (IBAction)unwindFromViewController2:(UIStoryboardSegue *)segue
{
    NSLog(@"and we are back");
}

Swift: b

@IBAction func unwindFromViewController2(segue: UIStoryboardSegue) {
    print("and we are back")
}

然后,您可以控制从视图控制器2中的按钮拖动到视图控制器上方的栏中的橙色退出图标故事板。在弹出窗口中,您可以选择 unwindFromViewController2 ,完成操作。

Then you'd control drag from the button in view controller 2 to the orange exit icon in the bar above the view controller in the Storyboard. In the pop up, you'd select unwindFromViewController2 and voila, you're done.

这篇关于故事板会导致内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆