Objective C – How to create a set of sub views to update

| | August 8, 2015

Perhaps I am thinking about this incorrectly, so please point me in the right direction..

I want to make many UIViews as a property so I can update them with different methods. Something like this:

@property UIView *view1

@property UIView *view2
...

@property UIView *view200

-(void)updateViews{

for (i=0; i <200; i++){

//Update view here

}

So my questions are:

  1. How can I easily make multiple views?
  2. How can I easily pass a message to each individual view?

So many great responses, I think I need to add a little more info.

Have tried putting my UIViews into an array to index them but I struggle to access them to update their positions within the UIViewController. I thought making them a property is the correct way to since I need to update them with the device heading. Looks like using a tag might be best. Can someone further comment on this approach:

for (i = 0 ; i < 144; i++) {
UIView *sunView =[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"sun.png"]];
    sunView.tag = i;
}

//then later, in a separate method do the following:

for (i = 0 ; i < 144; i++) {
CGFloat CGx = self.acceleration.X;
CGFloat CGy = self.acceleration.Y
[sunView.tag.i setFrame:CGRectMake(CGx, CGy, 20, 20)]
}

Im not sure how to access the UView with the corresponding indexed tag. Can someone help me there too?

4 Responses to “Objective C – How to create a set of sub views to update”

  1. Joe Hallenbeck on November 30, -0001 @ 12:00 AM

    How can I easily make multiple views?

    Do you want to create it in IB?

    1) You can use tags in Xcode

    enter image description here

    UILabel * label = (UILabel*)[self.view viewWithTag:19];

    UIView * view = (UIView*)[self.view viewWithTag:20];

    2) You can use property that you also do

    How can I easily pass a message to each individual view?
    if your views inside another view(currentView) you can use that

    for (UIView *yourView in self.currentView.subviews) {
        //Update view here
    }
    

    Or if you want to create it in the code

    for (int i = 0; i < 10; i++)
    {
          UIView * myView = [[UIView alloc]initWithFrame:CGRectMake(10*i, 10*i, 25, 25)];
          int r = arc4random() % 255;
          int g = arc4random() % 255;
          int b = arc4random() % 255;
          myView.backgroundColor = [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1];
         [self.view addSubview: myView];
    }
    

    That’s what you can see

    enter image description here

    And to call all these views you can use

     for (UIView *yourView in self.view.subviews) {
        [yourView removeFromSuperview];
    }
    

    And that’s what you’ll see

    enter image description here

    If you want to call own view you can use tags like this

     for (int i = 0; i < 10; i++)
         {
             UIView * myView = [[UIView alloc]initWithFrame:CGRectMake(10*i, 10*i, 25, 25)];
             myView.tag = i + 21;
             int r = arc4random() % 255;
             int g = arc4random() % 255;
             int b = arc4random() % 255;
             myView.backgroundColor = [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1];
             [self.view addSubview: myView];
         }
    
         for (UIView *yourView in self.view.subviews) {
             if (yourView.tag == 21) {
                 [yourView removeFromSuperview];
             }
         }
    
  2. Create your views in a loop and store them in an NSMutableArray. That way you will have access to all of them in your view controller.

    Create a property.

    @property (monatomic) NSMutableArray *arrayOfViews;
    
    
    self.arrayOfViews = [[NSMutableArray alloc] init];
    UIView *view;
    for (int i = 0; i < 10; i++)
    {
        view = [[UIView alloc] init];
        [self.arrayOfViews addObject:view];
    }
    

    Run a loop over this array and access the views one by one and call the method you need to call.

    On a side note: Why are you creating 200 UIViews? That’s a lot :)

  3. Hermann Klecker on November 30, -0001 @ 12:00 AM

    When they are really subviews then you could iterate through the superview’s subviews array.

    You could do that recursievely in order to apply it on all subivews of your subviews, if you have multiple hierarchy levels.

    If you don’t want to apply this to each of your superview’s subviews, then tag your subviews with something like view.tag = 6502 and use that to identify those views to which you want to apply it.

    Or maintain your own array or set (iterable collection) of views and iterate through them.

  4. I think you need something like that:

    -(void)updateViews
    {
        UIView *views;
        UILabel *label;
        for(int i = 0 ; i < numberOfViews ; i++)
        {
            views = [[UIView alloc] initWithFrame:CGRectMake(...)];
            label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,views.frame.size.width,views.frame.size.height)];
            [label setText:[NSString stringWithFormat:@"%d",i]];
            [views addSubview:label];
            [self.view addSubview:views];
        }
    }
    

Leave a Reply