`
lizaochengwen
  • 浏览: 644513 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

IOS5 parentViewController 问题

阅读更多
更新了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
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics