iOS:在多个自定义视图对象上设置变量

时间:2012-03-07 09:11:28

标签: ios variables uiview

好的,所以我正在创建X号的自定义UIView,我在IB中创建了... 我以类似网格的形式创建它们,需要根据来自Web服务调用的响应设置它们各自的属性...

我遇到问题的部分是如何遍历不同的UIViews并设置变量......

我很确定解决方案非常简单,但我已经盲目地盯着这一段时间......

这是之后的部分:

            if([theStatus.groupName isEqualToString:groupEntry.groupNameLabel.text])
            {

以下是整个方法:

- (void)receivedGroups
{
    int rows, columns;

    if([groupConnection.groupsArray count] <= 4)
    {
        rows = 1;
        columns = [groupConnection.groupsArray count];
    } else if([groupConnection.groupsArray count] >= 5 && [groupConnection.groupsArray count] <= 8)
    {
        rows = 2;
        columns = ([groupConnection.groupsArray count] + 1 )/ 2;
    } else
    {
        rows = 3;
        columns = ([groupConnection.groupsArray count] + 2 )/ 3;
    }

    int number = 0;

    for(int j=1; j < columns+1; j++)
    {
        for(int k=0; k < rows; k++)
        {
            // Only create the number of groups that match the number of entries in our array
            if(number < [groupConnection.groupsArray count])
            {
                // Create an instance of the group view
                GroupEntry *groupEntry = [[GroupEntry alloc] initWithFrame:CGRectMake(230*j, 250*k, 180, 233)];

                // Add it to the view
                [self.view addSubview:groupEntry];

                // Get the group
                GetGroupsActive *theGroups = [groupConnection.groupsArray objectAtIndex:number];

                groupEntry.groupNameLabel.text = theGroups.groupName;

                for(int i=0; i<[statusConnection.statusArray count]; i++)
                {                        
                    CurrentStatus *theStatus = [statusConnection.statusArray objectAtIndex:i];

                    if([theStatus.groupName isEqualToString:groupEntry.groupNameLabel.text])
                    {
                        //allChildren++;

                        switch(theStatus.currentStatus)
                        {
                            case 0:
                                //childrenSick++;
                                break;
                            case 1:
                                //childrenVacation++;
                                break;
                            case 2:
                                //childrenPresent++;
                                break;
                            case 3:
                                //childrenOut++;
                                break;
                            case 4:
                                //childrenTour++;
                                break;
                            default:
                                break;

                        }
                    }
                }

                NSString *allLabelText = [NSString stringWithFormat:@"%i", allChildren];
                NSString *sickLabelText = [NSString stringWithFormat:@"%i", childrenSick];
                NSString *vacationLabelText = [NSString stringWithFormat:@"%i", childrenVacation];
                NSString *presentLabelText = [NSString stringWithFormat:@"%i", childrenPresent];
                NSString *outLabelText = [NSString stringWithFormat:@"%i", childrenOut];
                NSString *tripLabelText = [NSString stringWithFormat:@"%i", childrenTour];

                groupEntry.sickLabelNumber.text = sickLabelText;
                groupEntry.presentLabelNumber.text = presentLabelText;
                groupEntry.numberLabelNumber.text = allLabelText;
                groupEntry.tripLabelNumber.text = tripLabelText;
                groupEntry.outLabelNumber.text = outLabelText;
                groupEntry.vacationLabelNumber.text = vacationLabelText;

                // Create the buttons to handle button press
                UIButton *childButton = [UIButton buttonWithType:UIButtonTypeCustom];

                childButton.frame = CGRectMake(230*j, 250*k, 180, 233);

                // Set an identity tag, so we can recognize it during button press
                childButton.tag = theGroups.ID;

                // When EventTouchUpInside, send an action to groupSelected:
                [childButton addTarget:self action:@selector(groupSelected:) forControlEvents:UIControlEventTouchUpInside];

                // Add it to the view
                [self.view addSubview:childButton];
            }

            number++;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

如果您在父视图中添加了所有视图。您可以使用

获取所有子视图
NSAarry *subviews = [base subviews];
for(UIView *subview in subviews)
{
subview.property = yourVaule;
}

您可以使用其标记或其他属性来区分子视图。

相关问题