switching between uiviewcontrollers without a UI navigator control

元气小坏坏 提交于 2019-12-05 04:45:30

问题


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.


回答1:


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




回答2:


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!)



来源:https://stackoverflow.com/questions/5963175/switching-between-uiviewcontrollers-without-a-ui-navigator-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!