简体   繁体   English

如何卸载菜单(UIViewControllers)

[英]How to unload menus (UIViewControllers)

I'm working on an app with which I send and receive data from and to a database. 我正在开发一个用于与数据库之间收发数据的应用程序。

At the moment, when I start the app in portrait or landscape position, it will always display the first menu in portrait style. 目前,当我以纵向或横向启动应用程序时,它将始终以纵向显示第一个菜单。 When I turn the first menu, it switches to landscape just fine. 当我打开第一个菜单时,它会切换到横向模式。 I have properties in my AppDelegate to store screen width and height. 我的AppDelegate中有属性来存储屏幕的宽度和高度。

Whenever I navigate to another menu for the first time, it takes the screen width and height from the AppDelegate to draw the new menu. 每当我第一次导航到另一个菜单时,它都会使用AppDelegate的屏幕宽度和高度来绘制新菜单。 Every menu I navigate to does this perfectly. 我导航到的每个菜单都可以完美完成。

The problem is, that when a menu is already loaded, I switch to another menu, turn the screen and go back to the previous menu, the layout is still in the previous screen mode. 问题是,当一个菜单已经加载时,我切换到另一个菜单,旋转屏幕并返回上一个菜单,布局仍然处于上一个屏幕模式。 For example: 例如:

  1. I start the app and get to the first menu in portrait mode. 我启动该应用程序,然后进入人像模式下的第一个菜单。 The values for screen width and height in the AppDelegate will change to the current view.bounds. AppDelegate中屏幕宽度和高度的值将更改为当前view.bounds。
  2. I navigate to menu #2 for the first time. 我第一次导航到菜单2。 Menu #2 will use the screen width and height in the AppDelegate to draw the menu. 菜单#2将使用AppDelegate中的屏幕宽度和高度绘制菜单。
  3. I navigate to menu #3 for the first time. 我第一次导航到菜单3。 Just like Menu #2, it takes the screen width and height from the AppDelegate to draw the menu. 就像菜单2一样,它需要从AppDelegate获得屏幕宽度和高度才能绘制菜单。
  4. I turn the menu from portrait to landscape. 我将菜单从纵向转到横向。 Menu #3 will redraw and change the screen width and height in the AppDelegate to the new values. 菜单#3将重绘并将AppDelegate中的屏幕宽度和高度更改为新值。
  5. I navigate back to menu #2, but it's still drawn as if it were in portrait mode. 我导航回菜单#2,但仍将其绘制为好像处于纵向模式。

Is there a way to unload the menu (that's not the obsolete method of "WillUnload" or "DidUnload")? 有没有一种方法可以卸载菜单(这不是“ WillUnload”或“ DidUnload”的过时方法)? Or is there a method that gets called every time I navigate to a menu that I already loaded in my memory? 还是每次我导航到已经加载到内存中的菜单时都会调用一种方法?

I love to hear from you. 我很高兴收到你的来信。

Love and regards, Björn 爱与问候,比约恩

  • viewWillAppear: method gets invoked every time the controller's view is about to be displayed. viewWillAppear:每次将要显示控制器的视图时,都会调用该方法。 Whenever you make a transition to and fro from a controller, it would be invoked. 每当您在控制器之间来回转换时,都将调用它。

    From Apple's documentation( iOS Developer Library ) : 从Apple的文档( iOS开发者库 )中:

    This method is called before the receiver's view is about to be added to a view hierarchy and before any animations are configured for showing the view. 在即将将接收者的视图添加到视图层次结构之前,并配置任何动画以显示视图之前,将调用此方法。 You can override this method to perform custom tasks associated with displaying the view. 您可以重写此方法以执行与显示视图关联的自定义任务。 For example, you might use this method to change the orientation or style of the status bar to coordinate with the orientation or style of the view being presented. 例如,您可能使用此方法来更改状态栏的方向或样式,以与要显示的视图的方向或样式相协调。 If you override this method, you must call super at some point in your implementation. 如果重写此方法,则必须在实现中的某个时刻调用super。

  • An another lifecycle method that you can use is viewWillLayoutSubviews . 您可以使用的另一个生命周期方法是viewWillLayoutSubviews You can read more about it here.( iOS Developer Library ) 您可以在此处了解更多信息。( iOS开发者库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM