I want to switch between 2 UIViewController classes programmatically without any extra UI control like a UITabBarController that adds a UI to the application.
My main loads the first view controller with addSubView.
vc1 = new viewc1();
window.AddSubview(vc1.View);
window.MakeKeyAndVisible ();
I can load my second viewcontroller from the first one with PresentModalViewController
vc2 = new viewc2();
PresentModalViewController(vc2, true);
but i need to switch back and forth, and to release the old viewControllers to save memory. What is the best way to do this? DismissModalViewControllerAnimated(false); in the 2nd view controller isnt releasing memory and I dont want modal "windows" as it doesnt seem optimal. I have a custom UI so the tabbar controller is not wanted.
You can do it in simple code. But you can't release the view controllers as it required to handle user interactions such as button tap events etc. Adding a view to window will only preserve view instance. If you release your view controller instance, you could get a bad access error or unrecognized selector error.
So let your main code be
And your switch code will be
Now in dealloc method add
Thats it...Hope this helps... I just followed your syntax
You can use a
UINavigationController
still, you don't need the extra UI that it provides. You can use the .Hidden
property of theUINavigationBar
to hide that. To switch between views you can just usePushViewController(controller, animated)
to push the new view. If you want to release the old controller then you can just set theUINavigationController
's.ViewControllers
property using:this will remove the reference of the first controller and make the second controller the root. (this will also work the other way around!)