user285594
user285594

Reputation:

Objective-C - is there any way to have UIView property as array?

I need to access the UIView array items (where all UIView will have its own independent activity). But while assigning as @property (assign, nonatomic) UIView *ui_view[MAXIMUM_UIVIEW];, its causing the build to fail.

is there anyway to make it work the way i am trying or some other better way?

ERROR:

enter image description here

CODE:

#import "ViewController.h"
const int MAXIMUM_UIVIEW = 50;

@interface ViewController ()

@property (assign, nonatomic) UIView *ui_view[MAXIMUM_UIVIEW];
@property (assign, nonatomic) NSInteger phone_height;
@property (assign, nonatomic) NSInteger phone_width;


-(void)func_draw_wall:(int)argument_count;

@end

@implementation ViewController

-(void)ui_button_submit:(UIButton*)sender {
  int __chronological__;

  //UIView *ui_view[MAXIMUM_UIVIEW];
  //int phone_height  =  self.view.frame.size.height;
  //int phone_width  =  self.view.frame.size.width;
  int phone_height  =  self.view.frame.size.height;
  int phone_width  =  self.view.frame.size.width;
  self.phone_height = phone_height;
  self.phone_width = phone_width;


  for (__chronological__ = 0 ; __chronological__ <= 50 ; __chronological__++ ) {
    [self func_draw_wall:__chronological__];
  }
}

-(void)func_draw_wall:(int)__chronological__ {

  self.ui_view[__chronological__] =
                        [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50,50)];

  [self.view addSubview:self.ui_view[__chronological__]];
  NSLog(@"Mem Address of localView[%d] = %x\n", __chronological__, self.ui_view);
}

-(void)ui_button {
  self.view.backgroundColor = [UIColor blackColor];
  UIButton *but = [[UIButton alloc] init];
  [but setTitle:@"--- Add ---" forState:UIControlStateNormal];
  [but setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
  [but addTarget:self action:@selector(ui_button_submit:)
        forControlEvents:UIControlEventTouchUpInside];

  int startHeight = 167;
  int frameHeight = self.view.frame.size.height - startHeight;
  [but setFrame:CGRectMake(0, startHeight, 320, frameHeight)];

  [but setTranslatesAutoresizingMaskIntoConstraints:false];
  [but setImage:[UIImage imageNamed:@"Image"] forState:UIControlStateNormal];
  [self.view addSubview:but];


  NSDictionary *viewsDict = @{@"button" : but};
  [self.view addConstraints:[NSLayoutConstraint
                           constraintsWithVisualFormat:@"V:[button]-0-|"
                           options:0 metrics:nil views:viewsDict]];

  NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:but
      attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual
      toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0
      constant:0];
  constraint.active = true;
  [self.view addConstraint:constraint];
  [self.view layoutIfNeeded];

}

- (void)viewDidLoad {
  [super viewDidLoad];
  [self ui_button];
}


@end

Upvotes: 0

Views: 568

Answers (1)

Lou Franco
Lou Franco

Reputation: 89172

Make the type NSArray *, not UIView *[]

You should be able to use a generic type parameter, so NSArray<UIView *> *

Upvotes: 3

Related Questions