2011-06-09 59 views
0

如果我添加一個UITextField到openGLView並再次移除它,dealloc永遠不會被調用。將UIView集成到cocos2d:如何發佈UIView元素?

// add a textfield to the openGLView 
codeTextfield = [[UITextField alloc] initWithFrame:codeTextfieldFrame]; 
[[[CCDirector sharedDirector] openGLView] addSubview:codeTextfield]; 

// remove the textfield 
[codeTextfield removeFromSuperview]; 

// call replaceScene 
[CCDirector sharedDirector] replaceScene:[Menu node]]; 

// dealloc will not be called 

我忍受了這個問題很長一段時間,目前還沒有解決方案。

+0

釋放它:

codeTextfield = [[UITextField alloc] initWithFrame:codeTextfieldFrame]; // RETAIN COUNT IS NOW 1 [[[CCDirector sharedDirector] openGLView] addSubview:codeTextfield]; // RETAIN COUNT IS NOW 2 [codeTextfield removeFromSuperview]; // RETAIN COUNT IS NOW 1 

爲了讓計數回0,從視圖中刪除codeTextfield後,而不是做這個。 – PengOne 2011-06-09 16:21:20

回答

2

檢查沿途的保留計數;這應該可以幫助你看到發生了什麼。你添加它作爲一個子視圖後

codeTextfield = [[UITextField alloc] initWithFrame:codeTextfieldFrame]; 
// RETAIN COUNT IS NOW 1 
[[[CCDirector sharedDirector] openGLView] addSubview:codeTextfield]; 
// RETAIN COUNT IS NOW 2 
[codeTextfield release]; 
// RETAIN COUNT IS NOW 1 

[codeTextfield removeFromSuperview]; 
// RETAIN COUNT IS NOW 0 -- DEALLOC WILL BE CALLED 
1

addSubview保留了該視圖。當你創建了alloc的對象時,你也擁有它。刪除視圖只會將保留計數減1。在將子視圖添加爲子視圖後,您需要[codeTextfield release]