0

All is in my title, i want to create some dynamics names, to allocate some differents UIViews but randomly. For example, i want to make :

Level1 * level1view = [[Level1 alloc] init];

So i try to make some NSString like this :

ClasseMap = [NSString stringWithFormat:@"Level%i", Map];
NomMap = [NSString stringWithFormat:@"level%iview", NumeroMap];
id nouveau = NSClassFromString(ClasseMap);
nouveau * ViewTest;
ViewTest = [self valueForKey:NomMap];
ViewTest  = [[nouveau alloc] init];

But it gives me : Use of undeclared identifier 'ViewTest'. How could i fix it please ? How could i get access to some variables (for example, level1view.example = YES;) ?

Thanks for your help ! =)

user2057209
  • 345
  • 1
  • 6
  • 19
  • 1
    see http://stackoverflow.com/questions/2226611/how-to-instantiate-an-object-of-class-from-string-in-objective-c – cli_hlt Mar 20 '13 at 23:05
  • But how to, for the name, like in my example "leve1view", because i put "NSClassFromString(ClasseMap) * NSClassFromString(NomMap) = [[NSClassFromString(ClasseMap) alloc] init];" but it's not working :( – user2057209 Mar 20 '13 at 23:11
  • I've edited my question because it was not so clear, but not duplicate :( – user2057209 Mar 22 '13 at 08:49

1 Answers1

0

why dont you rather keep an array of views instead?

int Map = 0;
int NumeroMap = 1;

NSMutableArray *array = [[NSMutableArray alloc] init];

//add your views (levels?) to the array
[array addObject:someUIView];
[array addObject:someOtherUIView];

//now you can use your indexes to access the views you want
UIView *yourView = [array objectAtIndex:Map];
UIView *anotherView = [array objectAtIndex:NumeroMap];

i think what you are trying to do is not the way to go about things.

Fonix
  • 11,447
  • 3
  • 45
  • 74
  • I can't take a rather of view because i'm creating an infinity game, so the algorithm choose which view he want to print, but they are not declared before (printing matters) – user2057209 Mar 20 '13 at 23:26