ios - How to programatically generate n UILabels and add to View? -


i newbie in ios development.

my goal to:

  1. generate n uilabels

    uilabel *label0 = [[uilabel alloc]init]

    uilabel *label1 = [[uilabel alloc]init]

    ...

    uilabel *label n = [[uilabel alloc]init];

  2. add each uilabel view using addsubview

to generate n uilabels (please correct me if doing in wrong way), declared nsmutabledictionay in viewcontroller.h

@property (strong,nonatomic)nsmutabledictionary *uilabelsdictionary; 

in viewcontroller.m

self.uilabelsdictionary = [[nsmutabledictionary alloc]init];  int yoffset = 40;  for(int i=0;i<10;i++) {     yoffset = yoffset+40;     [self.uilabelsdictionary setobject:[[uilabel alloc]initwithframe:cgrectmake(10, yoffset, self.view.frame.size.width, self.view.frame.size.width)] forkey:[nsstring stringwithformat:@"label%d",i]]; }  nslog(@"%@",self.uilabelsdictionary); 

i stuck on next step. how read each uilabel stored in dictionary uilabel , add view?

dictionary output

label0 = "<uilabel: 0x7f83dbd08ba0; frame = (10 80; 320 320); userinteractionenabled = no; layer = <_uilabellayer: 0x608000090680>>"; label1 = "<uilabel: 0x7f83dbc0f7b0; frame = (10 120; 320 320); userinteractionenabled = no; layer = <_uilabellayer: 0x60800008e240>>"; label2 = "<uilabel: 0x7f83dbc15570; frame = (10 160; 320 320); userinteractionenabled = no; layer = <_uilabellayer: 0x60800008e4c0>>"; label3 = "<uilabel: 0x7f83dbc159a0; frame = (10 200; 320 320); userinteractionenabled = no; layer = <_uilabellayer: 0x60800008e5b0>>";  label4 = "<uilabel: 0x7f83dbc15c30; frame = (10 240; 320 320); userinteractionenabled = no; layer = <_uilabellayer: 0x60800008e790>>";  label5 = "<uilabel: 0x7f83dbc16040; frame = (10 280; 320 320); userinteractionenabled = no; layer = <_uilabellayer: 0x60800008ebf0>>";  label6 = "<uilabel: 0x7f83dbc162d0; frame = (10 320; 320 320); userinteractionenabled = no; layer = <_uilabellayer: 0x60800008ef10>>";  label7 = "<uilabel: 0x7f83dbc16560; frame = (10 360; 320 320); userinteractionenabled = no; layer = <_uilabellayer: 0x60800008e510>>";  label8 = "<uilabel: 0x7f83dbc167f0; frame = (10 400; 320 320); userinteractionenabled = no; layer = <_uilabellayer: 0x60800008ec40>>";  label9 = "<uilabel: 0x7f83dbc16a80; frame = (10 440; 320 320); userinteractionenabled = no; layer = <_uilabellayer: 0x60800008f460>>"; 

use array.

@property (nonatomic, strong) nsarray *labels; 

and

nsmutablearray *labels = [nsmutablearray array];  (int i=0; i<10; i++) {     uilabel *label = [[uilabel alloc] initwithframe:cgrectmake(10, 80+i*40, self.view.frame.size.width, self.view.frame.size.width)];     [self.view addsubview:label]; // add label view     [labels addobject:label]; // keep track of labels } self.labels = labels;  nslog(@"%@", labels);  // later on... uilabel *label0 = self.labels[0]; // reference label 0 

Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -