How to get fixed number of subview from all subview of scrollview using fast enumeration in ios

86 views Asked by At

How to select fix number of subview from all subview of scrollview using fast enumeration in ios?

1

There are 1 answers

0
maddy On

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.