开发者

Integrate UIView to cocos2d: how to release UIView elements?

If I add a UITextField to the openGLView and remove it again, dealloc is never called.

// add a textfield to the openGLView
codeTextfield = [[UITextField alloc] initWithFrame:cod开发者_如何学运维eTextfieldFrame];
[[[CCDirector sharedDirector] openGLView] addSubview:codeTextfield];

// remove the textfield
[codeTextfield removeFromSuperview];

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

// dealloc will not be called

I suffer with this problem a long time and there is no solution in sight.


Check the retain counts along the way; this should help you to see what's going on.

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

To get the count back to 0 after removing codeTextfield from the view, instead do this:

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


addSubview retains the view. As you created the object with alloc, you own it too. Removing the view will only decrease the retain count by 1. You want [codeTextfield release] after you add it as a subview.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜