这是我创建子视图并将其添加到视图中的方式。
我想知道为什么计数总是返回0,它应该返回“数百”。我做错了什么,谢谢!
我添加了更多代码,清楚地显示了我的问题。我复制/粘贴了我最初问题中涉及的所有功能。
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.contenedor addSubview:vistaPanelBotones];
[self crearBotones];
}
- (void) crearBotones {
UIColor *colores[] = {
[UIColor blueColor],
[UIColor brownColor],
[UIColor redColor],
[UIColor orangeColor],
[UIColor greenColor],
[UIColor yellowColor],
[UIColor purpleColor],
[UIColor blackColor],
[UIColor whiteColor],
[UIColor darkGrayColor],
[UIColor magentaColor],
[UIColor cyanColor],
};
int indice = 0;
for (int col = 0; col < self.vistaPanelBotones.frame.size.width ; col=col+20) {
for (int fila = 0; fila < self.vistaPanelBotones.frame.size.height-20 ; fila = fila+20) {
CGRect frame = CGRectMake(col, fila, 20, 20);
Boton *boton = [Boton new];
boton.frame = frame;
boton.layer.backgroundColor = colores[(fila + col) % 7].CGColor;
boton.layer.cornerRadius = 0.25;
boton.layer.borderWidth = 0.25;
boton.layer.borderColor = [UIColor whiteColor].CGColor;
boton.layer.delegate = self;
[self.vistaPanelBotones addSubview:boton];
[boton setNeedsDisplay];
}
indice++;
}
NSLog(@"Vista Botones SubViews:%i",[[self.vistaPanelBotones subviews] count]);
}
- (IBAction)reiniciar:(id)sender {
if (self.vistaPanelBotones == nil){
NSLog(@"no existe la vista");
}
NSUInteger count = self.vistaPanelBotones.subviews.count;
NSLog(@"Vista SubViews: %i",count);
}
答案 0 :(得分:1)
以下是我的一些想法:
答案 1 :(得分:1)
使用[[self.vistaPanelBotones subviews] count]计算子视图的数量,但是有一种优雅的方法可以从Objective-C中的视图中删除所有子视图。试试这个:
[[self.vistaPanelBotones subviews] makeObjectsPerformSelector:@selector(removeFromSuperView];