0

i am adding view in mainview. how to find last inserted view width?

  NSArray *noPotions = [NSArray arrayWithObjects:@"1",@"2",@"3",@"4", nil];
int list = [noPotions count];

int lastPosition = 10;

int widthValue = 100;

for (int i = 0; i < list ; i++) {
    UIView  *newView = [[UIView alloc]initWithFrame:CGRectMake(lastPosition, 10, widthValue, 60)];
    [newView.layer setBorderColor:[[UIColor blueColor] CGColor]];
    [self.view addSubview:newView];
    lastPosition = newView.bounds.size.width + 20;
    newView.layer.borderWidth = 4;

    [newView release];

in this code last position always getting 120. anybody pls help me?

sreenivas
  • 399
  • 2
  • 5
  • 20

1 Answers1

0

You can always give tags to UIViews inside the FOR Loop first.

for (int i = 0; i < list ; i++) 
{
     UIView  *newView = [[UIView alloc]initWithFrame:CGRectMake(lastPosition, 10, widthValue, 60)];
     newView.tag = i;
     [newView.layer setBorderColor:[[UIColor blueColor] CGColor]];
     [self.view addSubview:newView];
     lastPosition = newView.bounds.size.width + 20;
     newView.layer.borderWidth = 4;

     [newView release];
}

Later on, to access any view you can always get it from their tag.

Supposing we want a view which is at the last element of your array....

UIView* latestView = [self.view viewWithTag:[list count]-1];
mayuur
  • 4,736
  • 4
  • 30
  • 65