Domanda

I'm quite new to iOS development and I'd love if you could share a bit of your experience with me.

I'm building an app that is basically a multimedia version of an 'Choose your own adventure' book. I have a view controller that shows some content, and when it receives interaction from the user (tapping a button for example) a new view controller is displayed.

The way I'm doing it goes like this:

I import the header files of the potentially next view controllers into the current view controller's header file. Then I define the IBActions to jump to the next controllers:

#import "path1ViewController.h"
#import "path2ViewController.h"
...
-(IBAction)goToPath1:(id)sender;
-(IBAction)goToPath2:(id)sender;
...

Then, in the implementation file, whenever the IBAction is fired I create an instance of the new controllers and push them into the navigation controller:

-(IBAction)goToPath1:(id)sender{  
    path1ViewController *path1VC = [[path1ViewController alloc] init];
    [self.navigationController pushViewController: path1VC];    
}

Is this a good approach for the app?

I also noticed that the memory consumed by the app increases when I jump back and forth between controllers, meaning that they are not being released. Any thoughts?

EDIT: This is what happens with the memory indicator when I switch between controllers. ARC is activated:

enter image description here

È stato utile?

Soluzione

Firstly your approach of is correct. UINavigationController is an stack of more than one viewcontroller.

-(IBAction)goToPath1:(id)sender{  
    path1ViewController *path1VC = [[path1ViewController alloc] init];
    [self.navigationController pushViewController: path1VC];    
}

Above lines are correct if you are using/enabled ARC in your project. Your memory management doing automatically by compiler.

If you choose non-ARC approach then you have to release your view-controller after pushing it into navigation controller stack then your code will be,

-(IBAction)goToPath1:(id)sender{  
    path1ViewController *path1VC = [[path1ViewController alloc] init];
    [self.navigationController pushViewController: path1VC]; 
    [path1VC release];   
}

In non-ARC way you must handle memory management manually.

Altri suggerimenti

For going between the controller this is most common approach to push your controller based on action but if you are facing the memory issue then just look for memory leak with Instrument or go for ARC!

Arc automatically release the object.your process is completed then u want to go back to main view then POP the view controller.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top