更新了XCODE4.2的朋友可能会奇怪,原来在XCODE4.0的时候一点问题都没有的代码,出现了不少的bug,其中比较典型的就是
[plain] view plaincopy
[UIViewController parentViewController]
返回nil。
苹果文档是这样写的
[html] view plaincopy
parentViewController
The parent of the current view controller. (read-only)
@property(nonatomic, readonly) UIViewController *parentViewController
Discussion
Parent view controllers are relevant in navigation, tab bar, and modal view controller hierarchies. In each of these hierarchies, the parent is the object responsible for displaying the current view controller. If you are using a view controller as a standalone object—that is, not as part of a view controller hierarchy—the value in this property is nil.
Prior to iOS 5.0, if a view did not have a parent view controller and was being presented modally, the view controller that was presenting it would be returned. This is no longer the case. You can get the presenting view controller using the presentingViewController property.
presentingViewController
The view controller that presented this view controller. (read-only)
@property(nonatomic, readonly) UIViewController *presentingViewController
Discussion
The default implementation of this property walks up the view hierarchy, starting from this view controller. The first view controller it finds that received the presentViewController:animated:completion: method, or that has its definesPresentationContext property set to YES is returned as the value of the property. It keeps walking up the hierarchy until it finds a value to return or it gets to the root view controller.
这个问题出来IOS5的SDK里面,IOS5新增了一个方法是拿 ViewController 的presentingViewController,在IOS4的时候,如果调用parentViewController的时候如果为空,它会一直网上找到顶层的presentingViewController,但是IOS5就把他们分开了。为了兼容IOS4和IOS5 直接调用这个方法来拿parentViewController。
[plain] view plaincopy
+(UIViewController*)getPresentingViewController:(UIViewController*)aViewController
{
if (!aViewController) {
NSLog(@"viewController is nil");
return nil;
}
UIViewController *presentingViewCtrl=nil;
if ([aViewController parentViewController]) {
//ios4
presentingViewCtrl = aViewController.parentViewController;
}else if([aViewController respondsToSelector:@selector(presentingViewController)]){
//ios5
if ([aViewController performSelector:@selector(presentingViewController)]) {
presentingViewCtrl = [aViewController performSelector:@selector(presentingViewController)];
}else{
NSLog(@"No presentingViewController");
}
}else{
NSLog(@"No presentingViewController");
}
return presentingViewCtrl;
}
原文地址:http://blog.csdn.net/a0199133/article/details/7048231
分享到:
相关推荐
知乎—ios5旧版本APP.知乎是网络问答社区,连接各行各业的用户。用户分享着彼此的知识、经验和见解,为中文互联网源源不断地提供多种多样的信息。准确地讲,知乎更像一个论坛:用户围绕着某一感兴趣的话题进行相关的...
美国Dave Mark、Jack Nutting、Jeff LaMarche编著的《iOS 5基础教程》是一部关于iOS SDK开发的基础教程,内容翔实、语言生动。几位作者结合消费类设备上的常见实例,循序渐进地讲解了适用于iPhone 4S、iPad 2及iPod ...
用于移动端和前端进行交互,H5页面获取iOS端点击事件,iOS端获取H5点击事件,实现实时交互
IOS5 CLPlacemark CLGeocoder
ios+android+h5复制剪切板
解决IOS问题(在home页面点返回时路由还是在home页面,并完成重新加载) 分析: 需要在进入页面时加入监听,并在页面离开时移除监听。因为home页面是个滚动列表,所以我采用了keep-alive路由缓存,所以不能在created...
ios开发 入门教程 适合新手学习交流 共同学习
资源名称:iphone、ipad应用开发-iOS5剖析(新增IOS5项目实战秦朝史话小教程内容:IOS5精品系列课程专题讲座:1.ios5精品系列课程专题讲座之开篇引言(Mac平台的使用)2.ios5精品系列课程专题讲座之开发之旅(Xcode的...
用HTML5开发iOS应用
ios5拨号界面,让ios6恢复成ios5
iPhone苹果iOS app开发教程合集(iOS5,iOS8).zip
Android 4 vs iOS 5
下面记录一下ios中ajax回调中播放音乐的问题解决过程 解决方案一 –降低音量 在用户点击时间后将audio的音量设置为0,等success后将音量设置为1; volume可以控制audio的音量大小,取值范围为 0~1 audio.v
iOS5程序开发pdf书籍,带有代码源码!
IOS5源代码 http://blog.csdn.net/dingxiaowei2013/article/details/10581543
【ios开发】IOS开发文档,帮着开发者学校IOS5,IOS开发文档,帮着开发者学校IOS5 (ios develop reference,help ios develop,ios develop reference,help ios develop)ios develop reference,help ios develop,...
ios5代码合集2,适合初学者参考学习,包含新技术
ios5 programming-cookbook配套代码,绝对正版.已经使用过,没有问题。
iOS5 Programming Cookbook中文版,完整版,整理好目录,不带水印。
ios5开发入门经典pdf文档,非国内盗名书籍。