2013-03-20 61 views
0

所有在我的标题,我想创建一些动态名称,分配一些differents UIViews,但随机。例如,我想打:为UIViews创建动态名称

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

所以我尝试做一些NSString的是这样的:

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]; 

但它给我:未声明的标识符“ViewTest”的使用。我该如何修复它?我怎样才能访问一些变量(例如,level1view.example = YES;)?

感谢您的帮助! =)

+1

请参阅http://stackoverflow.com/questions/2226611/how-to-instantiate-an-object-of-class-from-string-in-objective-c – 2013-03-20 23:05:38

+0

哦,非常感谢! – user2057209 2013-03-20 23:06:50

+0

但是,如何为名称,就像我的例子“leve1view”,因为我把“NSClassFromString(ClasseMap)* NSClassFromString(NomMap)= [[NSClassFromString(ClasseMap)alloc] init];”但它不工作:( – user2057209 2013-03-20 23:11:46

回答

0

为什么不宁愿保留一个视图数组呢?

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]; 

我认为你想要做的不是去做事情的方式。

+0

我不能采取一个相当的观点,因为我创建一个无限的游戏,所以算法选择哪个视图他想打印,但他们之前没有声明(打印事项) – user2057209 2013-03-20 23:26:12