xcode - 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 above code page navigate view2 page there not able access button.. in view2 pressed button got error

exe_bad_access(code=1 address=012902)

how release memory properly????

but using

[self.navigationcontroller pushviewcontroller:view2 animated:yes]; 

this popviewcontroller , pushviewcontroller not working ..(not able navigate view2 page)

anyone know reason????

the problem you're creating view controller, you're not using it, rather using view (but letting arc release view controller, itself, when goes out of scope). when transitioning between view controllers, should use pushviewcontroller (if you're using navigation controller) or presentmodalviewcontroller (if want present next view modally; in ios 5 use presentviewcontroller). in unlikely event using container view controller, use transitionfromviewcontroller transition between child view controllers. see view controller programming guide guidance on how transition between view controllers. in wwdc 2011 session 102, apple advises want ensure hierarchy of view controllers structured (generally mirroring view hierarchy); shouldn't adding views , ignore view controller hierarchy.

anyway, standard technique transitioning between views use navigation controller (that's gives title bar "back" button ... can hide if don't want 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 following line // [view2 release]; 

finally, if want use technique of adding view of new controller subview of current view controller's view (which doesn't seem idea), want make sure arc doesn't release when goes out of scope. so, don't let go out of scope making new view controller ivar of current view controller, not local var of current method. solves arc issue, doesn't seem technique, should doing proper transition between view controllers, present in interest of full disclosure.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -