Changing the tags during do-while loop
So i got a method to loop through an XML document. The document got a number of answervalues which are being extracted and are printed on the screen in the form of UISegmentedControls. The code i got right now is:
- (void) traverseElement:(TBXMLElement *)element {
static CGFloat y = 300.0f;
static CGFloat dynamicHeight = 400.0f;
static int segmentId = 1;
do {
if([[TBXML elementName:element] isEqualToString:@"wcqAnswerVal开发者_开发百科ues"]) {
NSString *segmentItemsStr = [TBXML textForElement:element];
NSArray *segmentItemsArray = [segmentItemsStr componentsSeparatedByString:@";"];
answer = [[UISegmentedControl alloc] initWithItems:segmentItemsArray];
answer.frame = CGRectMake(50, y, 400, 40);
[answer addTarget:self action:@selector(textpopup:) forControlEvents:UIControlEventValueChanged];
answer.segmentedControlStyle = UISegmentedControlStyleBar;
answer.tag = segmentId;
[scrollView addSubview:answer];
scrollView.contentSize = CGSizeMake(768, dynamicHeight);
[formulierText removeFromSuperview];
[answer release];
y += 40.0f;
dynamicHeight += 40.0f;
segmentId++;
}
if ([[TBXML elementName:element] isEqualToString:@"formdata"]) {
y = 300.0f;
dynamicHeight = 400.0f;
segmentId = 1;
}
// if the element has child elements, process them
if (element->firstChild)
[self traverseElement:element->firstChild];
// Obtain next sibling element
} while ((element = element->nextSibling));
}
-(void)textpopup:(UISegmentedControl *)sender {
if (sender.tag == 1) {
// if the tag of the sender is 1, then do something.
}
}
What i want to achieve is this:
Each UISegmentedControl that is being printed on the screen has to have a unique number or id so i can separately assign actions to them. What is the best way to achieve this?
EDIT: Fixed it. I will update my code.
精彩评论