开发者

How to distinguish dynamically generated button on selection using iPhone SDK?

In my iPhone app, I have array of buttons that are dynamically generated based on user selection.

How do I distinguish selected button from others?

I want that when user select the other button the previo开发者_开发知识库usly selected button should go back to its normal state in terms of its looks. I am unable to revert the previously selected buttons to its normal state.


Use tag to identify the button. At the time of creating buttons you can assign tag as number to the button and use the same to identify.

yourButton.tag = intNumber;


You have an array of the buttons. You can loop through your array and check if it is the one that was clicked.

- (IBAction) buttonClicked:(id)sender {
  for(int i; i < [array count]; i++){
     if((UIButton *)sender == (UIButton *)[array objectAtIndex:i])
        //do something
     else
        //do something else
}

Something like that.


Try setting tag for each button,using

yourButton.tag=intValue; //intValue>0

Your buttonAction should be as follows,

-(IBAction)buttonAction:(id)sender

Save the previously selected tag, and change the value accordingly.


you can loop through your subviews and set for all the old style:

- (void)highlightImgWithID:(int)packID {
    [self.view.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        if ([obj isKindOfClass:[UIImageView class]]) {
            [(UIImageView*)obj setHighlighted:([obj tag] == IDtoSelectNext)];
        }
    }];
}

The sample is how I currently implement it in my App with UIImageView's you can change it to work with buttons.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜