Created
June 22, 2012 18:42
-
-
Save pec1985/2974458 to your computer and use it in GitHub Desktop.
Titanium getElemetByTag()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Gets a Titanium Element | |
* @param {Ti.UI.View} Parent View - Where the view is | |
* @param {String} tag Property of the view | |
* @param {String} value Value of the custom property | |
* @return {Ti.UI.View} Return a view if found, otherwise null; | |
*/ | |
function getElementByTag(parent, tag, value){ | |
// Store the array in a variable for better performance | |
var childrenArray = parent.children; | |
// iterate through all the children | |
for(var i = 0, len = childrenArray.length; i < len; i++){ | |
// Store the child in a variable for better performance | |
var child = childrenArray[i]; | |
// get the custom property | |
var customProp = child[tag]; | |
// Two checks | |
// 1. That the prop exists | |
// 2. If so, that the prop is equal to the value | |
if(customProp && customProp == value){ | |
// return child view | |
return child; | |
} | |
} | |
// nothing found! | |
return null; | |
} | |
var view = Ti.UI.createView(); | |
for(var i = 0, i < 20; i++){ | |
view.add( | |
Ti.UI.createTextField({ | |
value: 'foo', | |
bar: 'id'+i | |
}) | |
); | |
} | |
var textField = getElementByTag(view, 'bar', 'id2'); | |
if(textField != null){ | |
// Do whatever | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment