tags:

views:

31

answers:

1

hi i am new to iphone. what i did is creating group of buttons in the view.My code is as fallows

- (void)mymethod1 {
    UIView *view = [[UIView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
        int row = 0;
        int column = 0;

        for (int i=6; i<_images.count; i++) {

            buttonimage = [UIButton buttonWithType:UIButtonTypeCustom];

            buttonimage.frame = CGRectMake(column*60+5, row*60+5, 70,70);
            [buttonimage setImage:[UIImage imageNamed:[_images objectAtIndex:i]] forState:UIControlStateNormal];
            buttonimage.tag = i;
            [buttonimage addTarget:self 
                       action:@selector(buttonClicked:) 
             forControlEvents:UIControlEventTouchUpInside];
            buttonimage.tag = i; 

            [view addSubview:buttonimage];

            if (column == 4) {
                column = 0;
                row++;
            } else {
                column++;
            }

        }

        self.view = view;
        [ view release];
}

it works fine. but when ever i call the all the buttons in any other functions it gets last button only for eg:

- (void)mymethod2 {
NSLog(@"button value %d",buttonimage.tag);

}

"it get in console button value 5".but i need to get all the button tag values in mymethod2. how can i get all button tag values.Please post some code.Thank u in advance.

+1  A: 

You need to iterate all the subviews of the current view and then check to see if it's a button before casting, something like this

for (UIView* view in [self.view subviews]) {
  if([view isKindOfClass:[UIButton class]]){
    UIButton *btn = (UIButton*)view; 
    NSLog(@"button value %d", btn.tag); 
  }
}
acqu13sce