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? DismissModalViewControllerAnima开发者_StackOverflow社区ted(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
if(vc1==nil)
vc1 = new viewC1();
window.addSubView(vc1.view);
window.MakeKeyAndVisible ();
And your switch code will be
if(vc2==nil)
vc2 = new viewC2();
if(vc1.view.superview!=nil){
vc1.view.removefromSuperView();
window.addsubview(vc2.view);
} else {
vc2.view.removeFromSuperView();
window.addsubview(vc1.view);
}
Now in dealloc method add
vc1.release();
vc2.release();
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 the UINavigationBar
to hide that. To switch between views you can just use PushViewController(controller, animated)
to push the new view. If you want to release the old controller then you can just set the UINavigationController
's .ViewControllers
property using:
navigationController.ViewControllers = new UIViewController[1]{ vc2 };
this will remove the reference of the first controller and make the second controller the root. (this will also work the other way around!)
精彩评论