IOS5 parentViewController 问题

本文探讨了iOS 5更新后,XCODE4.2中parentViewController返回nil的问题。介绍了parentViewController和presentingViewController的区别,并提供了一种兼容iOS 4和iOS 5的方法来获取正确的presentingViewController。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

更新了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.youkuaiyun.com/a0199133/article/details/7048231
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值