现在的位置: 首页 > 综合 > 正文

关于多个viewcontroller和nib文件旋转的问题

2017年10月31日 ⁄ 综合 ⁄ 共 1531字 ⁄ 字号 评论关闭

当有多个viewcontroller,且资源文件布局用nib时,旋转出了点问题。

现像:

一个rootviewcontroller管理所有的viewcontroller,需要里将对应的controller.view添加到rootcontroller.view上,而 windows只需要  [window addSubview:rootviewcontroller.view]

结果旋转时,即便每个viewcontroller配置好了旋转,也没有被调用。

原因:G到的原因说是,application 对window 的subview 也就是rootviewcontroller对应的controller发关了Rotate消息,面没有处理rootview的子view对应的controller,所以了view的布局,需要自己处理。

我的解决办法:

我就是为了偷懒,不想手动计算布局才用的资源(nib)文件。那么我省掉rootviewcontroller这个中间层,直接让appdelegate来管理windows的subview。

这个方法虽然比较土,但我还认可,起码可以偷懒。

参考内容:

t seems that UIApplication is dispatching a message to the active view controller.

But how does your View Controller instance get these messages?

The message is forwarded to the first view controller whose view has been added to the UIWindow instance.

This boils down to 3 basic scenarios:

  1. The ViewController whose view isadded directly to the UIWindowinstance (single view app)

  2. The navigation Controller in a Navigation based app, then the navigation controller forwards the message to the active views view controller.

  3. The tab bar Controller in a tab bar based app, then the tab bar controller forwards the message to the active views view controller (or the active navigation controller).

The problem you will have, is if you build an app with multiple views, but DO NOT use a Navigation controller or Tab Bar controller. If you swap views in and out of the UIWindow instance manually, you will not receive these messages reliably. This is similar to posts like this one:http://stackoverflow.com/questions/131062/iphone-viewwillappear-not-firing

Just use Apple's conventions for multiple views and you be fine. Hope this saves some one an hour or two

参考链接:http://stackoverflow.com/questions/548142/uiviewcontroller-rotate-methods

抱歉!评论已关闭.