Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-16414] iOS: removeAllChildren is not working until window is open

GitHub Issuen/a
TypeBug
PriorityMedium
StatusClosed
ResolutionFixed
Resolution Date2014-03-03T23:21:33.000+0000
Affected Version/sRelease 3.1.3, Release 3.2.0, Release 3.2.1
Fix Version/s2014 Sprint 05, 2014 Sprint 05 API, Release 3.3.0
ComponentsiOS
Labelsmodule_window, qe-testadded, supportTeam
ReporterMarco Cota
AssigneeVishal Duggal
Created2014-02-06T20:30:15.000+0000
Updated2014-07-31T07:49:02.000+0000

Description

Issue

In iOS when calling the removeAllChildren() method to a view part of a window that hasn't been open theres no action performed and the children of the view remain, in Android calling the same method before the window is open the children are removed correctly. Calling the view.remove(); before the window is open works correctly.

Test Case

Classic app.js
var win = Ti.UI.createWindow({backgroundColor:'green'});

var v = Ti.UI.createView({
	layout:'vertical'
});
var bt1 = Ti.UI.createButton({
title:'bt1',
top:10,
});
var bt2 = Ti.UI.createButton({
title:'bt2',
top:10});

v.add(bt1);
v.add(bt2);
win.add(v);

Ti.API.info('removing all children for content.  #children: ' +     v.children.length);
v.removeAllChildren();
Ti.API.info('... AFTER removing all children for content.  #children: ' +   v.children.length); 

win.open();
Alloy index.js
function setupMainWindow() {
    Ti.API.info('removing all children for content.  #children: ' +     $.content.children.length);
    $.content.removeAllChildren();
    Ti.API.info('... AFTER removing all children for content.  #children: ' +   $.content.children.length);
    var pickOne = Math.floor(Math.random()*5);

switch(pickOne){
    case 0:
        $.content.add($.pageOne);
        break;
    case 1:
        $.content.add($.pageTwo);
        break;
    case 2:
        $.content.add($.pageThree);
        break;
    case 3:
        $.content.add($.pageFour);
        break;
    case 4:
    default:
        $.content.add($.pageFive);
        break;
}
$.getView().open();
};

setupMainWindow();
index.xml
<Alloy>
    <Window backgroundColor="white" layout="vertical">
       	<View id="content" height="70%" width="70%" borderRadius="10">
            <View id="pageZero" height="10%" backgroundColor="black"><Label color="red">I should NEVER be visible</Label></View>
            <View id="pageOne"><Label>I am page one!</Label></View>
            <View id="pageTwo" layout="vertical"><Label>I am page two!</Label></View>
            <View id="pageThree" layout="horizontal"><Label>I am page three!</Label></View>
            <View id="pageFour"><Label bottom="5">I am page four!</Label></View>
            <View id="pageFive"><Label top="5">I am page five!</Label></View>
     	</View>
    </Window>
</Alloy>

Console log

Calling win.open() after the v.removeAllChildren() leaves the children.
[INFO] :   Focusing the iOS Simulator
[INFO] :   Application started
[INFO] :   testcase/1.0 (3.2.0.GA.d9182d6)
[INFO] :   removing all children for content.  #children: 2
[INFO] :   ... AFTER removing all children for content.  #children: 2
Calling win.open() before the v.removeAllChildren() correctly removes the children.
[INFO] :   Application started
[INFO] :   testcase/1.0 (3.2.0.GA.d9182d6)
[INFO] :   removing all children for content.  #children: 2
[INFO] :   ... AFTER removing all children for content.  #children: 0

Workaround

A simple loop can be used to read the children of a view and remove them, either if the window has been open or not.
function clearView(view){
	Ti.API.info('removing all children for content.  #children: ' +     view.children.length);
	var viewLength = view.children.length-1;
	for(var i = viewLength;i >= 0; i--){
		view.remove(view.children[i]);
	}
	Ti.API.info('... AFTER removing all children for content.  #children: ' +   view.children.length);	
};

Comments

  1. Ingo Muschenetz 2014-02-06

    [~mcota] Can you please add the Alloy use case to this ticket?
  2. Vishal Duggal 2014-03-03

    Pull pending against master https://github.com/appcelerator/titanium_mobile/pull/5408
  3. Priya Agarwal 2014-04-17

    Closing the issue as working as expected. removeAllChildren method working fine before window open. Test Environment: Appc-Studio:3.2.3.201404151910 sdk:3.3.0.v20140416200257 acs:1.0.14 alloy:1.3.1 npm:1.3.2 titanium:3.2.3-beta titanium-code-processor:1.1.1-beta1 xCODE:5.1.1 Device:Nexus7(v4.4.2),Iphone5(v7.1)

JSON Source