El desarrollo de aplicaciones para este dispositivo contiene la arquitectura del software MVC (model-view-controller)que nos ofrece aun serie de componentes para el usuario final mejor conocidos como Application Temples.
He decidido utilizar la plantilla View-based application, ya que nos permite utilizar el cambio de orientación del iPhone, lo cual es una característica única.
Comencemos el proyeco, File, New Project…, View-based application, nombremos el proyecto HelloiPhone y guardemos. Deberá iniciar nuestro marco de trabajo con los archivos fuente: HelloiPhoneAppDelegate.h, HelloiPhoneAppDelegate.m, HelloiPhoneViewController.h, HelloiPhoneViewController.m y main.m.
Archivo HelloiPhoneAppDelegate.h
#import <UIKit/UIKit.h>
@class HelloiPhoneViewController;
@interface HelloiPhoneAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
HelloiPhoneViewController *viewController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet HelloiPhoneViewController *viewController;
@endArchivo HelloiPhoneAppDelegate.m
#import "HelloiPhoneAppDelegate.h"
#import "HelloiPhoneViewController.h"
@implementation HelloiPhoneAppDelegate
@synthesize window;
@synthesize viewController;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Asignamos los límites de la ventana e iniciamos el controlador
CGRect screenBound = [[UIScreen mainScreen] bounds];
self.window = [[[UIWindow alloc] initWithFrame:screenBound] autorelease];
viewController = [[HelloiPhoneViewController alloc] init];
// Override point for customization after app launch
[window addSubview:viewController.view];
[window makeKeyAndVisible];
}
- (void)dealloc {
[viewController release];
[window release];
[super dealloc];
}
@end
Archivo HelloiPhoneViewController.h
#import <UIKit/UIKit.h>
// Importamos la librería del componente de texto
#import <UIKit/UITextView.h>
@interface HelloiPhoneViewController : UIViewController {
// Declaramos nuestros mensajes y el componente de texto
NSString *holaMundo;
NSString *wow;
UITextView *texto;
}
@endHelloiPhoneViewController.m
#import "HelloiPhoneViewController.h"
@implementation HelloiPhoneViewController
// Iniciamos nuestras variables
- (id)init
{
self = [super init];
if(self != nil)
{
holaMundo = [[NSString alloc] initWithString: @"Hola, Mundo!!!"];
wow = [[NSString alloc] initWithString: @"WoW, ¿Qué fue eso Mundo?"];
}
return self;
}
// Cargamos la vista
- (void)loadView
{
[super loadView];
texto = [[UITextView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
texto.text = holaMundo;
self.view = texto;
}
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
*/
// Solo hay que cambiar el valor del retorno
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return YES;
}
// Agregamos la función que se encarga de la orientación
- (void)didRotateFromInterfaceOrientation: (UIInterfaceOrientation) fromInterfaceOrientation
{
texto.text = wow;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}
- (void)dealloc {
[holaMundo release];
[wow release];
[texto release];
[super dealloc];
}
@end
Archivo main.m
#import <UIKit/UIKit.h>
int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, @"HelloiPhoneAppDelegate");
[pool release];
return retVal;
}
One response to “Hello World, iPhone SDK con Xcode”
Hablas del modelo vista controlador e implementas programaticamente la creacion de la interfaz grafica sin el interface builder me gustaria tener una explicacion de el manejo y asociacion del interfaz builder.
Gracias de antemano muy buen articulo