I've an array which holds different kinds of objects: UIButton
s, UILabel
s, UITableView
s, etc.
Is there any way that I can dynamically create these objects while looping through the array without using if
/else
conditions like below:
for (NSObject *obj in objectsArray)
{
if ([obj isKindOfClass:[UIButton class]])
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.view addSubview:btn];
}
else if ([obj isKindOfClass:[UILabel class]])
{
UILabel *lbl = (UILabel*)obj;
[self.view addSubview:lbl];
}
}
Can we create objects like UIButton *btn
or UILabel *lbl
using reflection or something dynamically?