cocos2d-x 2.1.1 在ios 下獲取 rootviewcontroller 的問題。

一般 原生的

[[UIApplication sharedApplication].keyWindow.rootViewController presentModalViewController:self animated:NO];

可以 獲取  系統的  rootviewcontroller

 

但 cocos2d-x 2.1.1 在 appcontroller.mm 內定義的 加載方法是

// Set RootViewController to window
    if ( [[UIDevice currentDevice].systemVersion floatValue] < 6.0)
    {
        // warning: addSubView doesn't work on iOS6
        [window addSubview: viewController.view];
    }
    else
    {
        // use this method on ios6
        [window setRootViewController:viewController];
    }

 

也就是說  只有在 ios6 下 才設置rootview  其他時候是 使用addsubview的方法 加載。

 

所以 相應的 獲取 rootviewcontroller方法 要改爲。

if ( [[UIDevice currentDevice].systemVersion floatValue] < 6.0)
    {
        // warning: addSubView doesn't work on iOS6
        NSArray* array=[[UIApplication sharedApplication]windows];
        UIWindow* win=[array objectAtIndex:0];
       
        UIView* ui=[[win subviews] objectAtIndex:0];
        UIViewController* ctrol=(UIViewController*)[ui nextResponder];
    }
    else
    {
        // use this method on ios6
       
UIViewController* ctrol=[UIApplication sharedApplication].keyWindow.rootViewController];
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章