2

I'm creating an app with XCode 6 (Swift) and recently managed to make a custom segue that doesn't use an animation. What I soon realised was that, when switching between two view controllers (tied together with the custom segues), the application memory usage (viewable with XCode) seemed drastically increase with every view controller switch. Is this something that I need to worry about? Does Swift automatically take care this memory increase, so that the app doesn't end up using an insane amount, or am I doing something wrong?

NoAnimationSegue.swift

import Foundation
import UIKit

@objc(NoAnimationSegue)
class NoAnimationSegue: UIStoryboardSegue {

override func perform () {
    let src: UIViewController = self.sourceViewController as UIViewController
    let dst: UIViewController = self.destinationViewController as UIViewController

    src.presentViewController(dst, animated: false, completion: nil)

}
}
Tice
  • 103
  • 1
  • 10
  • First rule: test only with a release build, on a device. All other results are meaningless. Second rule: implement `deinit` to log and make sure your view controllers are going out of existence as expected. – matt Jun 08 '15 at 23:19
  • Out of curiosity, why are you defining a custom segue that just does `presentViewController`? If that's all you're going to do, then just use `presentViewController` directly without any animation. Or define a segue without animation. There are a bunch of ways of doing it, but I wouldn't be inclined to create a custom segue like this. – Rob Jun 09 '15 at 00:42
  • Truth is that I am very new to Swift and app programming in general. Anyway, thank you for the tip! – Tice Jun 09 '15 at 12:32

1 Answers1

1

When you go from the first view controller to the second, you can presentViewController, but when you go back to the first, you do not want to "present" again. When you "present" one view controller from another, it keeps the first one in memory, so it's there, ready for you when you "dismiss" the second view controller to return back to the first.

Thus, if you repeat that process of presenting from one view controller to another in a circular fashion, you'll end up with multiple instances of the view controllers in memory. If you "present" to go from one view controller to another, then you'll want to unwind or dismiss to get back to the first view controller.

Alternatively, if you don't want to use the present/dismiss pattern, you can alternatively use tab bar controller if you want to jump back and forth between view controllers. Or you can use page view controller. Or you can use your own custom container view controller and replace the child view controller as you jump back and forth.

But just remember, if you present or push from one view controller to another, then you'll have to dismiss or pop to return back.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • How would I unwind to return to the previous view controller? – Tice Jun 09 '15 at 12:07
  • Cool. How would I go about removing the animation that comes with unwinding the segue? – Tice Jun 09 '15 at 20:17
  • The handling of non-standard animations with unwind segues is a bit flaky, IMHO, so if you don't want animation when you dismiss the second view controller, the easiest approach would be to not use unwind segue, and instead just create `@IBAction` like so: `@IBAction func didTapDismissButton(sender: AnyObject) { dismissViewControllerAnimated(false, completion: nil) }` and hook up your button to that `@IBAction` instead. No custom segues. No unwind segues. Just simple `@IBAction`. – Rob Jun 10 '15 at 02:07
  • Works like a charm. Thank you! – Tice Jun 10 '15 at 11:28