jueves, 24 de mayo de 2012

Hacer múltiples Vistas iOS

Intro.
En varias ocaciones, nos encontraremos en la situación en la que buscamos hacer multiples vistas, o que tenemos que crear varias imagenes en la pantalla. Así que uno como principiante lo que haría es crear vista por vista o imagen por imagen, hasta terminar con nuestro objetivo.

Solución:
Como comentado mas arriba, me he encontrado con la sitacion que debo de crear multiples vistas y que en estas mismas deben de contener una imagen o un label. La solucion es la combinación de un plist y estas agregarlas a una vista, donde en el plist lo tendremos persoxnalizado con los valores que buscamos poner o crear. Como por ejemplo: el tamaño, el nombre, direcciones de la imagen, entre otras.



Ejemplo:
En este ejemplo combinaremos la entrada del plist y gestor.
Plist:

Nosotros tenemos aquí un plist, donde le doy el nombre de la imagen y sus medidas

Código:
El código lo estamos reutilizando así que no explicaré mucho de el ya que tengo entradas donde las explico.
-(void)plistVista
{
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *plistName = @"vistas.plist";
NSString *finalPath = [path stringByAppendingPathComponent:plistName];
NSDictionary *plistData = [NSDictionary dictionaryWithContentsOfFile:finalPath];
int cplistData = plistData.count;
for(int i = 0; i < cplistData; i++)
{
NSDictionary *vistaMenu = [plistData objectForKey:[NSString stringWithFormat:@"v%d", i+1]];
NSString *nombreVista = [vistaMenu objectForKey:@"nombre"];
NSLog(@"%@", nombreVista);
NSDictionary *frame = [vistaMenu objectForKey:@"frame"];
NSString *xMargin = [frame objectForKey:@"xMargin"];
float fxMargin = xMargin.floatValue;
//NSLog(@"%@", fxMargin);
NSString *yMargin = [frame objectForKey:@"yMargin"];
float fyMargin = yMargin.floatValue;
//NSLog(@"%@", fyMargin);
NSString *width = [frame objectForKey:@"width"];
float fwidth = width.floatValue;
//NSLog(@"%@", fwidth);
NSString *height = [frame objectForKey:@"height"];
float fheight = height.floatValue;
//NSLog(@"%@", fheight);
[self crearVista:fxMargin :fyMargin :fwidth :fheight :nombreVista]; //Mandamos llamar el metodo
//donde crearemos las vistas mandando el nombre y sus tamaños
}
}
view raw gistfile1.m hosted with ❤ by GitHub

-(void)crearVista:(float)x :(float)y :(float)ancho :(float)largo :(NSString*)nombreVista
{
//Creamos vista
viewMenu = [[UIView alloc] initWithFrame:CGRectMake(x, y, ancho, largo)];
//viewMenu.multipleTouchEnabled = FALSE;
viewMenu.exclusiveTouch = YES;
UILabel *labelVistas = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 150, 30)];
labelVistas.text = nombreVista;
[labelVistas setTextColor:[UIColor whiteColor]];
[labelVistas setBackgroundColor:[UIColor clearColor]];
[labelVistas setFont:[UIFont fontWithName: @"Helvetica Neue" size: 24.0f]];
[viewMenu addSubview:labelVistas];
[self.view addSubview:viewMenu];
//Agregamos gestor
UITapGestureRecognizer *dtouch = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(nombreVista:)];
dtouch.accessibilityLabel = nombreVista;
[viewMenu addGestureRecognizer:dtouch];
}
view raw gistfile1.m hosted with ❤ by GitHub

Resultado:

El resultado podemos ver que es el mismo si tuvieramos que crear vista por vista con sus determinados parámetros.

No hay comentarios:

Publicar un comentario