views:

67

answers:

3

Is it possible to dynamically build a property or function call? I have a set of views I want to render in the same manner. So if part of my code is like this

self.ViewName.hidden = NO;

and I want to use a variable for the name of the view, is there a way to do it, something like

self{var}.hidden = NO;

Where 'var' is a NSString of the view name and evaluated at runtime? I know this won't work with the angle brackets, just to give of how I am trying to build the property reference.

Thanks

+1  A: 

You can dynamically get a selector at run time using the NSSelectorFromString function. So if you wanted to get the viewName based on a string you would use

[[self performSelector:NSSelectorFromString(@"ViewName")] setHidden:NO];
DHamrick
Thanks DHamrick, that worked perfectly
Bryan Sorensen
Just don't forget to check if 'self' responds to selector you create, otherwise your app may crash if your viewname is incorrect
Vladimir
A: 

You can use setValue:forKeyPath: method:

NSString* path = [NSString stringWithFormat:@"%@.hidden", viewName];
[self setValue:[NSNumber numberWithBool:YES] forKeyPath:path];
Vladimir
A: 

If you have multiple views, you should put them in an array and access each element of the array separately.

NSMutableArray * views...
[[views objectAtIndex:i] setHidden:NO];
Cappuccino Plus Plus