Bharathi D
Bharathi D

Reputation: 1013

How to release and retain memory properly for view controllers using ARC ? - iOS

using ARC feature

record_audio_testViewController *view2 = [[record_audio_testViewController alloc] initWithNibName:@"record_audio_testViewController" bundle:nil];

    [self.view addSubview:view2.view];

using the above code the page is navigate to view2 page but there i am not able access any button.. in view2 i pressed a button i got error

Exe_Bad_Access(code=1 address=012902)

how to release a memory properly????

but using

[self.navigationController pushViewController:view2 animated:YES];

this popviewController and pushViewController not working ..(NOT ABLE TO NAVIGATE VIEW2 PAGE)

anyone know the reason????

Upvotes: 0

Views: 3406

Answers (3)

Darren
Darren

Reputation: 10398

In your .h set the property of record_audio_testViewController *view2 Either strong or weak. (Possibly strong)

Also, make sure you have set the files owner class to record_audio_testViewController in your xib.

Upvotes: 1

self
self

Reputation: 1215

record_audio_testViewController *view2 = [[[record_audio_testViewController alloc] initWithNibName:@"record_audio_testViewController" bundle:nil]autorelease]; 
[self.view addSubview:view2];

Upvotes: 0

Rob
Rob

Reputation: 437552

The problem is that you're creating the view controller, but you're not using it, but rather only using its view (but letting ARC release the view controller, itself, when it goes out of scope). When transitioning between view controllers, you should use pushViewController (if you're using a navigation controller) or presentModalViewController (if you want to present the next view modally; in iOS 5 use presentViewController). In the unlikely event you are using a container view controller, you could even use transitionFromViewController to transition between child view controllers. See View Controller Programming Guide for guidance on how to transition between view controllers. But in WWDC 2011 session 102, Apple clearly advises that you want to ensure that your hierarchy of view controllers be properly structured (generally mirroring your view hierarchy); you shouldn't be just adding views and ignore the view controller hierarchy.

Anyway, the standard technique for transitioning between views is to use a navigation controller (that's what gives you the title bar with the "back" button ... you can hide it if you don't want it visible):

record_audio_testViewController *view2 = [[record_audio_testViewController alloc] initWithNibName:@"record_audio_testViewController" bundle:nil];

[self.navigationController pushViewController:view2 animated:YES];

// if non-ARC project, uncomment the following line
// [view2 release];

Finally, if you really want to use your technique of adding the view of your new controller as a subview of your current view controller's view (which doesn't seem like a good idea), you just want to make sure that ARC doesn't release it when it goes out of scope. So, don't let it go out of scope by making the new view controller an ivar of your current view controller, not a local var of your current method. That solves the ARC issue, but doesn't seem like a good technique, as you probably should be doing a proper transition between view controllers, but I present it in the interest of full disclosure.

Upvotes: 2

Related Questions