0

我正在使用 Cocos2d 制作 iOS 游戏,需要自定义 UITableView 的外观。看来 这篇文章提供了一个很好的框架,当我自己测试时,示例代码运行良好。问题是我无法让它在我的 cocos2d 应用程序中工作。

该示例使用此代码创建自定义 UIViewController:(window 是 UIWindow,viewController 是 EasyCustomTableController)

[window addSubview:viewController.view];
[window makeKeyAndVisible];

但是当我使用该代码时,EasyCustomTableController 的 viewDidLoad 函数永远不会运行,我的游戏中也没有任何反应。我可以使用以下代码运行 viewDidLoad 函数:

levelMenu = [[EasyCustomTableController alloc] init];
[[[CCDirector sharedDirector] openGLView] addSubview:levelMenu.view];

但同样,我的游戏中什么也没有发生,视图也从未出现过。

如何让自定义 UIViewController 在我的 cocos2d 应用程序中工作?

编辑:

我可以得到一个白色背景的UIView来显示,所以levelMenu.view肯定有问题:

UIApplication* clientApp = [UIApplication sharedApplication];
UIWindow* topWindow = [clientApp keyWindow];
if (!topWindow) {
    topWindow = [[clientApp windows] objectAtIndex:0];
} 
//Works
UIView *white = [[UIView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
white.backgroundColor = [UIColor colorWithRed:255 green:255 blue:255 alpha:255];
[topWindow addSubview:white];

//Doesn't work
levelMenu = [[EasyCustomTableController alloc] init];
[topWindow addSubview:levelMenu.view];

我没有从示例中更改 levelMenu 的类代码 - 您可以在此处的第一个代码框中看到viewDidLoad

4

3 回答 3

1

尝试这个

UIApplication* clientApp = [UIApplication sharedApplication];   
UIWindow* topWindow = [clientApp keyWindow];
if (!topWindow) {
    topWindow = [[clientApp windows] objectAtIndex:0];
}
[topWindow addSubview:levelMenu.view];

如果您没有看到任何内容,则可能是 levelMenu.view 有问题。尝试使用带有白色背景的简单基本 UIView 来测试它。

于 2012-08-30T08:40:01.027 回答
0

首先你制作 CCLayer 并制作我们在 cocos2d 中制作的 Pushscreen。

[[CCDirector sharedDirector] pushScene:[ScoreLayer node]];

在这一层 ScoreLayer.h

    @interface ScoreLayer : CCLayer <UITableViewDelegate,UITableViewDataSource>{
        AppController *myApp;
        UIView *view;
        UITableView *tblView;
 UIToolbar *toolbar;
    UIBarButtonItem *backButton;
    NSMutableArray *ScoreArray;

    }
    @property(nonatomic,retain) UITableView *tblView;

评分层.m

-(id) init
{
    myApp = (AppController *)[UIApplication sharedApplication].delegate;
    self = [super init];
    // create view
    view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];

    // create Table
    tblView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 436)];
    tblView.delegate = self;
    tblView.dataSource = self;
   // tblView.style = UITableViewStyleGrouped;
    //table.style = UITableViewStyleGrouped;

    ScoreArray = [myApp getScoreList];
    [view addSubview:tblView];
toolbar = [UIToolbar new];
    [toolbar setFrame:CGRectMake(0, 436, 320, 44)];
    toolbar.barStyle = UIBarStyleBlackOpaque;

    //Create a button
    backButton = [[UIBarButtonItem alloc] initWithTitle:@"Return to the main menu" style:UIBarButtonItemStyleBordered target:self action:@selector(backClicked:)];

    [toolbar setItems:[NSArray arrayWithObjects:backButton,nil] animated:YES];
    [view addSubview:toolbar];
    [toolbar release];

    // add the view to the director
    [[[CCDirector sharedDirector] openGLView] addSubview:view];

    return self;
}
于 2012-08-30T07:14:15.777 回答
0

尝试这个

[[[CCDirector sharedDirector] openGLView].superview addSubview:levelMenu.view];
于 2012-08-30T03:42:11.000 回答