iOS:iOS 4.3和5.0之间的不同addSubview行为 [英] iOS: different addSubview behavior between iOS 4.3 and 5.0

查看:77
本文介绍了iOS:iOS 4.3和5.0之间的不同addSubview行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iOS 4.3编码之前,我发现在使用 [superview addSubView:controller.view] 将视图控制器的视图添加到另一个视图时,控制器实例将不会收到 -viewWillAppear / viewDidAppear 消息,比我在堆栈溢出中的某个线程中发现了同样的问题。之后,我根据需要手动调用 -viewWillAppear / -viewDidAppear

while coding in iOS 4.3 before, I found while add a view controller's view to another view with [superview addSubView:controller.view], the controller instance will not receive the -viewWillAppear/viewDidAppear message, than I found same issue in some thread in stack overflow. After that, I manually call -viewWillAppear/-viewDidAppear as needed.

但是,升级到 iOS 5.0 后,一些活泼的 UIView 行为发生了。最后我发现在iOS 5中, [superview addSubView:controller.view] ,将发送 -viewWillAppear / -viewDidAppear 自动向控制器实例发送消息,加上我的手动调用,每次控制器执行其行为时都会有两条重复的消息。

but, after upgrade to iOS 5.0, some frisky UIView behavior happened. Finally I found that in iOS 5, the [superview addSubView:controller.view] , will send a -viewWillAppear/-viewDidAppear message to the controller instance automatically, plus my manually calls, there are two duplicated message each time the controller action its behavior.

我也发现了类似的问题: iOS 5:-viewWillAppear未被调用在解雇iPad中的模式后

and I also found a similar issue: iOS 5 : -viewWillAppear is not called after dismissing the modal in iPad

现在,问题是,在搜索苹果的文档之后,我没有找到关于这些问题的任何明确的文档。我甚至怀疑这是否是iOS 5.0中保证的视图生命周期行为。

Now, the problem is, after search apple's documents, I didn't find any explicitly doc for diff about these issues. I even wonder if this is a guaranteed view life cycle behavior in iOS 5.0 .

是否有人解决类似问题或找到有关这些差异的指导原则。因为我想在 4.x&中运行我的应用程序5.x iOS

Does anyone fix similar issues or find some guidelines about these difference. cause I want to run my app both in 4.x & 5.x iOS.

推荐答案

在iOS 4中你必须手动调用 -viewWillAppear , -viewWillDisappear 等。如果在窗口层次结构中添加或删除视图,则会在iOS 5中自动调用它们。幸运的是,iOS 5在 UIViewController 中有一个方法可以覆盖以将行为恢复为与iOS 4一起工作的方式。只需将其添加到 UIViewController

In iOS 4 you had to manually call -viewWillAppear, -viewWillDisappear, etc. when adding or removing a view from your view hierarchy. These are called automatically in iOS 5 if the view is being added or removed from the window hierarchy. Fortunately, iOS 5 has a method in UIViewController that you can override to revert the behaviour back to how it worked with iOS 4. Just add this to your UIViewController:

-(BOOL)automaticallyForwardAppearanceAndRotationMethodsToChildViewControllers {
   return NO;
}

这可能是最简单的解决方案,只要你支持iOS 4 5.一旦您放弃对iOS 4的支持,您可以考虑修改代码以在交换视图时使用更新的方法。

This is probably the easiest solution as long as you're supporting both iOS 4 and iOS 5. Once you drop support for iOS 4 you might consider modifying your code to use the newer approach when swapping views.

编辑2012年2月5日

显然这个函数需要使用 addChildViewController:方法将子视图控制器添加到主视图控制器中。 iOS4中不存在此方法,因此您需要执行以下操作:

Apparently this function requires the child view controller be added to the main view controller using the addChildViewController: method. This method doesn't exist in iOS4, so you need to do something like this:

  if ([self respondsToSelector:@selector(addChildViewController:)] ) {
     [self addChildViewController:childViewController];
  }

感谢所有纠正我的人。

这篇关于iOS:iOS 4.3和5.0之间的不同addSubview行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆