Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
696 views
in Technique[技术] by (71.8m points)

ios - Storyboard segues causing memory leaks

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. storyboard

profiler 1

profiler 2 project's settings

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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

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]

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:

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

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.

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...