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

本文讨论了iOS应用中多个viewcontroller使用nib布局时遇到的旋转问题。通过分析问题产生的原因,作者提出了一种解决方案,即去掉rootviewcontroller层,直接由appdelegate管理windows的subview,确保viewcontroller能够正确接收到旋转消息。

当有多个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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值