How to get viewController of AppController?

How to get viewController of AppController?
0.0 0



I want to add a game center LeaderBoard modal view on my game.

That’s why I need the viewController of AppController.

I have a temporal solution that I write a function in AppController so that I can get the viewController direct.

Do I have another elegant way to do this?

Thank you.


viewController is assigned to window.rootViewController, I think you can get it from window.


Hi, Minggo:

I knew that rootViewController is assigned to widnow.

But I also have no idea how to get the window on my class.




I found it, just note it, in case someone like me a newbie want to know how to do it.

-(UIViewController*) getRootViewController
return [UIApplication sharedApplication].keyWindow.rootViewController;


I recently work with UMeng Add-in


void test()
UIViewController* ctrol;
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];
ctrol=(UIViewController*)[ui nextResponder];
// use this method on ios6
ctrol = [UIApplication sharedApplication].keyWindow.rootViewController;
if (m_pBanner == nil) {
m_pBanner = [[UMUFPBannerView alloc] initWithFrame:CGRectMake(0, 44, 320, 50) appKey:UmengAppkey slotId:nil currentViewController:ctrol];
[ctrol.view addSubview:m_pBanner];
[m_pBanner requestPromoterDataInBackground];


you can see the ctrol is rootViewController. these code work, but when i want to get some response (through adding some delegates to the ctrol)

it seems very difficult.

I add delegate into at proj.ios folder), it doesn’t seems work.
so i think the [UIApplication sharedApplication].keyWindow.rootViewController is not create by rootViewController.

so i want to get a the member (RootViewController *viewController of AppController which locate at proj.ios folder).
but i don’t know what can i do to get the instance of AppController in test method.

test method is in a mm file.(C++ and Object C file) which independent of AppController.

any suggestion, thank you.


Excellent post zhlhmeng! You saved me hours of head scratching. Building on your one liner to get the root view controller, I was able to get the Game Center multiplayer screen up by just replacing the word “self” in the sample code posted on Apple’s site with yours. Before it was crashing badly.

       + ( void )showMatch {
    GKMatchRequest *request = [[GKMatchRequest alloc] init];
    request.minPlayers = 2;
    request.maxPlayers = 4;
    GKTurnBasedMatchmakerViewController *mmvc = [[GKTurnBasedMatchmakerViewController alloc] initWithMatchRequest:request];
    mmvc.turnBasedMatchmakerDelegate = self;
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:mmvc animated:YES completion:NULL];