开发者

how to get all button tag values from a group of buttons

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开发者_如何学C post some code.Thank u in advance.


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); 
  }
}


All button tag values:-

for(int i=0;i<3;i++){
     UIButton *theButton=[[UIButton alloc]init];
     theButton.tag=i;

    //set their selector using add selector
    [theButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
    //set their frame color title or image
   }

-(void)buttonClicked:(UIButton *)inButton{
       int tags=inbutton.tag;
}


UIButton *BtnTmp=sender;
if([BtnTmp tag])
    [BtnTmp setImage:[UIImage imageNamed:@"check.png"] forState:UIControlStateNormal];
else
    [BtnTmp setImage:[UIImage imageNamed:@"uncheck.png"] forState:UIControlStateNormal];

BtnTmp.tag=!BtnTmp.tag;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜