我想在 cocos2d 项目中显示 UIViewController,所以我在我的 CCLayer 类中这样做:
-(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 我这样做:
- (IBAction)exitAction:(id)sender
{
[self.view removeFromSuperview];
[[CCDirector sharedDirector] pushScene: [MainMenu scene]]; //i need it or not?
}
一切正常,我使用 cocos2d v2.0,但我想知道是否有更好的方法在 cocos2d 场景中添加 UIViewController,谢谢!