在cocos2d中添加UIViewController [英] Add UIViewController in cocos2d

查看:178
本文介绍了在cocos2d中添加UIViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在cocos2d项目中显示一个UIViewController,所以我在CCLayer类中这样做:

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

然后在我的ViewController中返回我的CCLayer, p>

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?
}

并且所有工作,我使用cocos2d v2.0,但我想知道有一个更好的方法来添加一个UIViewController在cocos2d场景中,感谢!

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!

推荐答案

因为cocos2d 2.0的导演是一个子类的UIViewController作为在AppDelegate中加载的导航控制器的根,实际上有一个非常简单的方法可以使用UIKit动画,如果你喜欢。它还保留了场景,而不必担心内存管理。你可以改变你的-loadMyViewController方法到这:

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

然后只需恢复director并调用[app.navController popViewController ... ]当你想返回cocos2d场景。或者,您可以在推视图控制器而不是暂停时使用stopAnimation方法,类似于cocos2d处理应用程序进入背景的方式。

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.

这篇关于在cocos2d中添加UIViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆