lunes, 26 de agosto de 2013

UIViewController - UINavigationController

(contenido parcial, pendiente de finalizar)

 UIViewController 


La presentación y gestión de Views se realiza mediante UIViewController. 

UIViewController gestiona la relación entre la View y los datos (model).

UIView es el contenido mostrado al usuario, el Model son los datos gestionados por la aplicación, y el UIViewController es el intermediario entre esos componentes, así se forma el Model-View-Controller.


Si creamos un UIViewController y le nombramos RootViewController. En el fichero .h del application delegate.

#import <UIkit/UIkit.h>

@class RootViewController  // referencia al UIViewControlle creado

@interface AplicacionAppDelegate: UIRespondeer <UIApplicationDelegate>

@property (nonatomic, strong) UIWindows *window;
@property (nonatomic, strong) RootViewController *rootViewController;

@end

Ya tenemos declarada un propiedad RooViewController que es la clase UIViewController que hemos creado en el XCode mediante File New ...



Ahora en el fichero .m del application delegate en el método didFinishLaunchingWithOptions instanciamos el UIViewController que hemos creado y se lo añadimos a nuestro windows.

- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{


self.windows = [[UIWindow alloc] 
initWithFrame:[UIScreen mainScreen] bounds]];


[self.windows makeAndVisible];

self.rootViewController = [[RootViewController alloc]
initWithNibName:nil
bundle:NULL];

[self.window addSubview:self.rootViewController.view];

return YES;

}

Ya hemos iniciado la aplicación con nuestro UIViewController creado.

En la creación del UIViewController se indico que "With XIB for user interface" en este caso XCode creo un fichero que podemos ver el diseño en interface builder. Podemos cargar ese fichero en el método didFinishLaunchingWithOptions del application delegate.

En lugar de:

self.rootViewController = [[RootViewController alloc]
initWithNibName:nil
bundle:NULL];

Quedaría así, indicando en nombre del fichero .xib:

self.rootViewController = [[RootViewController alloc]
initWithNibName:@"RootViewController"
bundle:NULL];



 UINavigationController 


UINavigationController permite al usuario desplazarse por los distintos UIViewController de la aplicación. 

Realiza llamadas push y pop para añadir y extraer elementos UIViewController que gestiona para que sean visualizados. El UIViewController situado arriba de la pila es la pantalla que visualiza el usuario.

Añadiremos un UINavigationController. En el dichero .h del app delegate añadimos una property UINavigationController.


#import <UIkit/UIkit.h>

@class RootViewController  // referencia al UIViewControlle creado

@interface AplicacionAppDelegate: UIRespondeer <UIApplicationDelegate>

@property (nonatomic, strong) UIWindows *window;
@property (nonatomic, strong) RootViewController *rootViewController;
@property (nonatomic, strong) UINavigationController *navigationController;

@end


Inicializamos el UINavigationController con el método initWithRootViewController pasandole el rootViewController.



- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{

self.windows = [[UIWindow alloc] 
initWithFrame:[UIScreen mainScreen] bounds]];


[self.windows makeAndVisible];

self.rootViewController = [[RootViewController alloc]
initWithNibName:nil
bundle:NULL];

self.navigationController = [[UINavigationController alloc]
initWithRootViewController:self.rootViewController];

[self.window addSubview:self.navigationController.view];

return YES;

}

Ahora tendremos una pantalla con una barra de navegación en la que podemos colocar botones.



Crearemos un segundo UIViewController que lo añadiremos a nuestro NavigationController. En el fichero .m de implementación del RootViewContoller tendremos la llamada a esta segunda UIVIewController.

#import "RootViewController.h"
#import "NuevoViewController.h"

@implementation RootViewController....

En el método viewDidApperar del rootViewController.

- (void) viewDidAppear:(BOOL)paramAnimated{

 [super viewDidAppear:paramAnimated];
 [self performSelector:@selector(pushNuevoViewController)
withObject:nil
afterDelay:3.0f];
}

En el método pushNuevoViewController

- (void) pushNuevoViewController{

NuevoViewController *nuevoViewController = [[NuevoViewController alloc]
initWithNibName:nil
bundle:NULL];        


[self.navigationController 
pushViewController:nuevoViewController 
animated:YES];                                 

}


Ahora ya tenemos un nuevo UIViewController en la pila del UINavigationCotroller añadido mediate pushViewController

Para eliminarlo de la pila tenemos que utilizar el método popViewControllerAnimated.

En el método viewDidAppear del UIViewController nuevoViewController añadir este código.

- (void) viewDidAppear:(BOOL)paramAnimated{

[super viewDidAppear:paramAnimated];

[self performSelector:@selector(decargarPila)
withObject:nil       
afeterDelay:10.0f];

}

- (void) descargarPila
{
  [self.navigationController popViewControllerAnimated:YES];
}
Después de descargar nuevoViewController aparecerá rootViewController que es el UIViewController que pasará a ser el UIViewController que está en la parte superior de la pila.
















2 comentarios: