Jin
Jin

Reputation: 143

Loading uiviews efficiently on iphone sdk

I have 20 uiviews class that are labelled "Question1","Question2","Question3" and etc all the way to "Question20".

In my main controller, I tried to have a method called nextQuestion and pass an argument of the current question number to it. However, I wish to find a way to alloc init the correct Question view.

I ended up doing something like this which is not correct.

UIView *nextLevel = [[[NSString stringWithFormat:@"Question%i",levelNow] alloc]initWithFrame:CGRectMake(0, 0, 480, 320)];

Could someone help me with this? Thanks

Upvotes: 0

Views: 61

Answers (1)

Codo
Codo

Reputation: 78815

You create a string and then you call alloc on it. However, alloc can only be used on classes. So you're missing the step going from a string to a class. This gap can be closed with NSClassFromString:

Class viewClass = NSClassFromString([NSString stringWithFormat:@"Question%i",levelNow]);
UIView *nextLevel = [[viewClass alloc] initWithFrame:CGRectMake(0, 0, 480, 320)];

Upvotes: 1

Related Questions