cocos2d-x 2.1.1 在ios 下获取 rootviewcontroller 的问题。
更新:HHH   时间:2023-1-7


一般 原生的

[[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];
    }

 

返回游戏开发教程...