iOS.Lover
iOS.Lover

Reputation: 6051

Storyboard segues and receiving memory warning

I am developing an application with iOS 9 based SDK , this is my first time I am working with Storyboards , I have 20 view controllers, each scene has Next / Previous buttons to go back and forward . I have a huge problem with going forward !. If I move from scene 1 to for example to scene 15 I received memory warning and then application crashes . I have searched and it seems there is method called unwind segue but it seems this is for going back ! it's something like dissMiss method .

I connect each scene with line in Interface Builder :

enter image description here

Here is segue's setting :

enter image description here

I would be grateful if you help me out .

EDITED :

I tried to present a view controller programmatically but result was the same ! .

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    WhatIsDino *vc = (WhatIsDino*)[mainStoryboard instantiateViewControllerWithIdentifier:@"WID"];
    [self presentViewController:vc animated:YES completion:nil];

Upvotes: 1

Views: 292

Answers (3)

Duncan C
Duncan C

Reputation: 131398

The problem is that you are adding view controller after view controller with modal presentation. That causes each view controller to be added on top of the previous one, and all of them accumulate, using more and more memory.

Using a navigation controller and a push also piles the view controllers on top of each other.

You will have this problem if you use storyboards, nibs, or create the view controllers manually.

If you have a design where the user can move through a large series of view controllers then you probably want to dismiss the previous one before pushing/presenting a new one.

You can probably dismiss the previous view controller without animation and then present the new view controller each time you want to display a new one and avoid the memory issue. i'd have to experiment with it to get the effect I was after, but that's what I would suggest.

Upvotes: 0

Amit Ajmera
Amit Ajmera

Reputation: 1467

You can also navigate without segue and Its easy way I think.

If you want to navigate from Class1 to Class 2 then follow these steps.

1) In Class 1, Import Class2.

2) In your button Action, Write this code.

Class2 *next = [self.storyboard instantiateViewControllerWithIdentifier:@"Class2 Identifier name"]; [self.navigationController pushViewController:next animated:YES];

Do not forget to give Identifier name in story board that is "Storyboard ID" in Attribute inspector of particular class.

No need to add Segue,Your storyboard would look clean.

Upvotes: 0

ReDetection
ReDetection

Reputation: 3186

Seems like it's a problem of wrong approach, and not the storyboard.

Let me guess, since before storyboard you used to change your app's rootViewController to the next/previous screen once you tap on the arrow button. So previous screen are released and deallocated from memory once you set a new rootViewController.

And now you're presenting every next view controller modally, which involved creating new UIWindow and loads all the hierarchy of you screen and keeps previous underneath the new one so it holds the memory and you're getting out of memory crash.

Well, you can do rootViewController approach with a storyboard too since it's just another way to manage your screens while development. Storyboard offers additional features like segues, static table view cells, general tint color and so on. [UIStoryboard -instantiateViewControllerWithIdentifier:] is the method you might find interesting.

But I'd rather recommend you to check out the UIPageViewController, it's like a container for the screens. Unfortunately, it cannot have the segues to your scenes (because of the special way segues work) so you have to use -instantiateViewControllerWithIdentifier: method anyway. You can treat inner view controllers of UIPageViewController as you do with rootViewController before.

Upvotes: 2

Related Questions