2012-08-29 47 views
0

我正在使用Cocos2d制作iOS游戏,并且需要自定义UITableView的外观。看起来this post提供了一个很好的框架,并且我自己测试时,示例代码运行良好。问题是我无法在我的cocos2d应用程序中运行它。Cocos2d中的自定义UIView-iphone

示例使用此代码来创建自定义的UIViewController:(窗口是一个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中的第一个代码框中here

回答

0

试试这个

[[[CCDirector sharedDirector] openGLView].superview addSubview:levelMenu.view]; 
+0

viewDidLoad运行,我的游戏停止处理输入,但表视图仍然没有出现。 – Cbas

+0

在'viewDidLoad'中尝试'bringSubviewToFront:'。还要确保'view.frame'是正确的 –

+0

我的编辑中的新代码应该将视图置于前面。我尝试设置levelMenu.tableView.frame来适应屏幕,但这并没有改变什么 – Cbas

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; 

ScoreLayer.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; 
} 
+0

什么是AppController?我注释掉了这些行,并在 - [ScoreLayer tableView:numberOfRowsInSection:] – Cbas

+0

oye它的我的委托对象,你刚刚删除它,并从该。AppController * myApp的.h文件声明得到'NSInvalidArgumentException' – Nims

+0

把表格视图dalegate的方法在课堂上..你打电话,但你不放在.m文件 – Nims

1

试试这个

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

如果你什么也没看到,再有就是可能是levelMenu.view的问题。试试用白色背景的简单基本UIView来测试它。

+0

相同的错误白色UIView出现,但levelMenu.view没有。我完全没有从示例http:// cocoawithlove中更改该代码。com/2009/04/easy-custom-uitableview-drawing.html – Cbas

+0

您是否尝试过这样做:“代码在顶部包含一个#define,它允许您打开和关闭自定义绘图。” ? – jptsetung

+0

把它关掉没有区别。我的输入锁定了,但视图没有出现 – Cbas

相关问题