Piero
Piero

Reputation: 9273

Add UIViewController in cocos2d

i want to display a UIViewController in a cocos2d project, so i have do this in my CCLayer class:

-(void)displayMainMenu {
    CGSize screenSize = [CCDirector sharedDirector].winSize;
    [CCMenuItemFont setFontName:@"Marker Felt"];
    [CCMenuItemFont setFontSize:26];

    CCMenuItemFont *openViewC = [CCMenuItemFont itemWithString:@"Open View" target:self selector:@selector(loadMyViewController)];
    mainMenu = [CCMenu menuWithItems:openViewC, nil];
    [self addChild:mainMenu z:0];
}

-(void) loadMyViewController{

    //Add the tableview when the transition is done
    myView = [[MyViewController alloc] init];
    UIView *viewHost = hostView.view;

    [[[CCDirector sharedDirector] view] addSubview:viewHost];
}

and then in my ViewController to return to my CCLayer i do this:

- (IBAction)exitAction:(id)sender
    {
    [self.view removeFromSuperview];

    [[CCDirector sharedDirector] pushScene: [MainMenu scene]]; //i need it or not?
}

and all work, i use cocos2d v2.0, but i want know if there is a better way to add a UIViewController in a cocos2d scene, thanks!

Upvotes: 3

Views: 7734

Answers (4)

Alexios Nixon
Alexios Nixon

Reputation: 27

OR you can use this code..

ViewController *vc = [[ViewController alloc] init];

AppController *app = (AppController*) [[UIApplication sharedApplication] delegate];

[[app navController] presentModalViewController:vc animated:YES];

Upvotes: 1

Alexios Nixon
Alexios Nixon

Reputation: 27

You can use a simple code if you want to pop up a view , just import yourviewcontroller.h file and use this code that supports all cocos2d versions

- (void)loadMyViewController {

     yourviewcontroller  *myView = [[yourviewcontroller alloc] init];

     UIView *viewHost = myView.view;

    [[[CCDirector sharedDirector] openGLView] addSubview:viewHost];

}

//Replace the screen if you want to reduce memory usage or just pause it

Upvotes: 2

Corbin87
Corbin87

Reputation: 538

Since the director in cocos2d 2.0 is a subclass of UIViewController as the root of a navigation controller loaded in the AppDelegate, there is actually a very simple way that can utilize UIKit animations, if you prefer that. It also leaves the scene intact without really worrying about memory management. You could alter your -loadMyViewController method to this:

- (void)loadMyViewController {
     myView = [[MyViewController alloc] init];
     AppController *app = (AppController *)[[UIApplication sharedApplication] delegate];
     [app.navController pushViewController:myView animated:YES];
     [CCDirector sharedDirector].pause;
}

And then simply resume the director and call [app.navController popViewController...] when you want to return to the cocos2d scene. Alternatively, you could use the stopAnimation method when you push your view controller instead of pause, similar to how cocos2d handles the app entering the background.

Upvotes: 9

Guru
Guru

Reputation: 22042

If it is cocos2D 2.0, then use this.

AppController *app = (AppController*) [[UIApplication sharedApplication] delegate];
        [app.navController.view addSubview:newView];

Bellow cocos2d 2.0

AppDelegate* app = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    [app .viewController.view addSubview:newView];

Upvotes: 1

Related Questions