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

查看:16
本文介绍了在 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 我这样做:

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,但我想知道是否有更好的方法可以在 cocos2d 场景中添加 UIViewController,谢谢!

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中的director是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;
}

然后在想要返回 cocos2d 场景时简单地恢复 director 并调用 [app.navController popViewController...].或者,您可以在推送视图控制器而不是暂停时使用 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天全站免登陆