2012-08-29 63 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

相關問題