How to select fix number of subview from all subview of scrollview using fast enumeration in ios?
Asked
Active
Viewed 84 times
0
-
Please provide details explanation - What you want to achieve, What you have done, What problem you faced, What you expected to do. – Kampai Oct 09 '14 at 10:30
-
I have one scrollview that consist of ten dynamically generated buttons. I want to access only first 5 buttons from scrollview using fast enumeration. so please provide some solution to get only 5 buttons from the loop like "for(UIButton *btn in scrollview)". – Aashi Oct 09 '14 at 10:51
-
please provide more detail varsha – maddy Oct 09 '14 at 10:51
1 Answers
0
do as following: 1.give tags to your buttons when you are creating them.for example:
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.do not understand why you want to use fast enumeration, you can simple get your buttons by:
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.if your requirement is to using fast enumeration, take an NSMutableArray to hold your buttons:
NSMutableArray *arrayOfButtons = [[NSMutableArray alloc] initWithCapacity:5];
for (id btn in self.yourScrollView.subviews)
{
@autoreleasepool {
if (btn.tag > 0 || btn.tag<=5)
{
[arrayOfButtons addObject:btn];
}
}
}
hope it will help you.

maddy
- 4,001
- 8
- 42
- 65