如何使用 ios 中的快速枚举从滚动视图的所有子视图中选择固定数量的子视图?
问问题
81 次
1 回答
0
执行以下操作: 1.在创建按钮时为按钮添加标签。例如:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.tag = 1;
[button addTarget:self
action:@selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:button];
2.不明白为什么要使用快速枚举,您可以通过以下方式简单地获取您的按钮:
UIButton *btn1 = (UIButton *)[self.yourScrollView viewWithTag:1];
UIButton *btn2 = (UIButton *)[self.yourScrollView viewWithTag:2];
UIButton *btn3 = (UIButton *)[self.yourScrollView viewWithTag:3];
UIButton *btn4 = (UIButton *)[self.yourScrollView viewWithTag:4];
UIButton *btn5 = (UIButton *)[self.yourScrollView viewWithTag:5];
2.如果您的要求是使用快速枚举,请使用 NSMutableArray 来保存您的按钮:
NSMutableArray *arrayOfButtons = [[NSMutableArray alloc] initWithCapacity:5];
for (id btn in self.yourScrollView.subviews)
{
@autoreleasepool {
if (btn.tag > 0 || btn.tag<=5)
{
[arrayOfButtons addObject:btn];
}
}
}
希望它会帮助你。
于 2014-10-09T11:04:56.680 回答