Note that there are some explanatory texts on larger screens.

plurals
  1. POInstantiating a set amount of unique objects from a single method
    text
    copied!<p>This is something I've just never been able to get my brain around...</p> <p>Let's say I am building a grid of 8 UIViews wide by 8 UIViews high. It could by any x, y set. It doesn't have to be 8 x 8. Let's just be arbitrary and stick to that for now. </p> <p>This init method (part of a subclass of UIView) generates a row 8 UIViews wide:</p> <pre><code> - (id)initWithFrame:(CGRect)frame { if ((self = [super initWithFrame:frame])) { int x = 1; int y = 1; //row 01 UIView* row01Square01 = [[UIView alloc] initWithFrame:CGRectMake((0*x), (0*y), x, y)]; [self addSubview:row01Square01]; UIView* row01Square02 = [[UIView alloc] initWithFrame:CGRectMake((1*x), (0*y), x, y)]; [self addSubview:row01Square02]; UIView* row01Square03 = [[UIView alloc] initWithFrame:CGRectMake((2*x), (0*y), x, y)]; [self addSubview:row01Square03]; UIView* row01Square04 = [[UIView alloc] initWithFrame:CGRectMake((3*x), (0*y), x, y)]; [self addSubview:row01Square04]; UIView* row01Square05 = [[UIView alloc] initWithFrame:CGRectMake((4*x), (0*y), x, y)]; [self addSubview:row01Square05]; UIView* row01Square06 = [[UIView alloc] initWithFrame:CGRectMake((5*x), (0*y), x, y)]; [self addSubview:row01Square06]; UIView* row01Square07 = [[UIView alloc] initWithFrame:CGRectMake((6*x), (0*y), x, y)]; [self addSubview:row01Square07]; UIView* row01Square08 = [[UIView alloc] initWithFrame:CGRectMake((7*x), (0*y), x, y)]; [self addSubview:row01Square08]; } return self; </code></pre> <p>}</p> <p>Is it possible to write a method that could use that code (with modifications of course) to generate the subsequent 7 rows of UIViews? Better yet, is it possible to use 1 line of to generate all 64 UI Views?</p> <p>I have experimented with using a for, while, do loop, but I admit I am completely lost when it comes to whether you or not you can pass a string as an argument to an init method.</p> <p>Thanks in advance for any insight into this.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload