0

enter code hereI have two view-controller.when i move to one view controller to another view controller,(void)viewDidUnload function not calling automatically. so that it was increasing my memory allocation of app.after 2,3 time moving between these view-controller my application getting stuck.

when I use ios7 simulator it not stuck. how can i release unused memory? how can I call (void)viewDidUnload function automatically when i move to second view controller?
this is memory usage.it increase when I navigate between two view controller

button click event

[self performSegueWithIdentifier:@"linktoviewmeetingitem" sender:tableView];

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"linkquicksearch"]) {
        ViewControllerSearch *searchScreen = [[ViewControllerSearch alloc] initWithNibName:@"ViewControllerSearch" bundle:nil];
        searchScreen = (ViewControllerSearch*)segue.destinationViewController;
        [searchScreen setSearchString:_txtSearch.text];
        searchScreen = nil;
    }
}
Suravi
  • 301
  • 1
  • 7
  • 21
  • How do you navigate between these view controllers? Show some code. – Mick MacCallum Nov 04 '13 at 11:43
  • 1
    This method is DEPRECATED in iOS6 ... also even back then viewDidUnload is only called when your viewController is deallocated, not on a transition to another view controller. - you sound like you are incorrectly transitioning, alloc initing an already instanciated controller. – Woodstock Nov 04 '13 at 11:44

3 Answers3

1

This method is deprecated in iOS6 ... also even back then viewDidUnload is only called when your viewController is deallocated, not during/after a transition to another view controller. - You are instantiating an already instantiated viewController each time you 'transition' - leading to your memory issue.

You need to wrap the [alloc init] of your viewController in an if statement to check if said viewController instance already exists.

From the docs: viewDidUnload Called when the controller’s view is released from memory. (Deprecated in iOS 6.0. Views are no longer purged under low-memory conditions and so this method is never called.) - (void)viewDidUnload Discussion In iOS 5 and earlier, when a low-memory condition occurred and the current view controller’s views were not needed, the system could opt to call this method after the view controller’s view had been released. This method was your chance to perform any final cleanup. If your view controller stored separate references to the view or its subviews, you could use this method to release those references. You could also use this method to remove references to any objects that you created to support the view but that are no longer needed now that the view is gone. You would not use this method to release user data or any other information that cannot be easily recreated. In iOS 6 and later, clearing references to views and other objects in your view controller is unnecessary. At the time this method is called, the view property is nil.

Woodstock
  • 22,184
  • 15
  • 80
  • 118
0

Just because you navigate away from the viewcontroller, it won't be unloaded. if you want to catch the event when the viewcontroller becomes navigated away from, use the

- (void)viewWillDisappear:(BOOL)animated;

function if you want to catch it before happens or the

- (void)viewDidDisappear:(BOOL)animated;

if you want to catch the event after.

Zoltan Varadi
  • 2,468
  • 2
  • 34
  • 51
  • how to remove current view controller from memory? – Suravi Nov 04 '13 at 11:57
  • ARC will release it automatically once no strong reference points to it. this means if you pop a viewcontroller it will probably get removed from the memory. but if you push an other one onto it it won't. – Zoltan Varadi Nov 04 '13 at 12:08
0

Basically on the iOS7 and ARC never people use a - (void)viewDidUnload{}

but you can use to force the memory unlock in this mode example:

in your .h have a IBOutlet like this:

@property (strong, nonatomic) IBOutlet UISlider *progressSlider;

than in your .m file you can use:

- (void)viewDidUnload {
    [super viewDidUnload];
    [self setProgressSlider:nil];
}

Hope this help you

BlackSheep
  • 1,087
  • 12
  • 29
  • Yes I did like this.but memory not decrease when transition.how can I reduce the memory – Suravi Nov 04 '13 at 12:18
  • try to implementing my code with a - (void)viewDidDisappear:(BOOL)animated; – BlackSheep Nov 04 '13 at 12:20
  • thank your advice.my problem is how to deallocate current viewcontroller in - (void)viewDidDisappear:(BOOL)animated; – Suravi Nov 04 '13 at 12:25
  • there is other way, but i don't like, i explain: you can remove ARC for a single file .m and use the Release and Dealloc: see my post here:http://stackoverflow.com/questions/15702604/ios-arc-property-readwrite-nonatomic-vs-radwrite-retain-nonatomic/17389952#17389952 – BlackSheep Nov 04 '13 at 12:29