2011-06-09 61 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]