Xcode Lectia a doua HelloWord in Objective-C utilizand Storyborad

Creăm proiectul

Pentru a începe cu crearea de noi applicatii iOS, vom crea un proiect nou.

Deschidem Xcode, când va fi încărcat, veţi vedea ecranul de bun venit.

În această fereastra, faceţi clic pe “Create a new Xcode project”.

Se va deschide fereastra ce conține template-urile.

Selectăm “Emthy Application”.

Pagina următoare vă va permite să definiţi numele proiectului şi identificatorul. Identificator este numele aplicaţiei. Îl puteţi vedea ca un nume de domeniu scris inversat. Dacă domenul site-ului dvs. este mycompany.com, identificatorul dvs. ar fi com.mycompany.APPLICATIONNAME.

 

Nu uitam sa deselectam “Use Core Data” si “Include Unit Test“.

În baza locaţiei pe care o selectaţi și a numelui proiectului, va fi creat un dosar cu acel nume şi fişierele proiectului nostru vor fi fixate acolo. Imediat după salvarea template-ului, vei observa cum iți este organizată fereastra de lucru.

 

Incepem sa programam

Deschidem fișierul “AppDelegate.h” si declarăm o nouă variabilă ”helloLabel” de tipul “UILabel

//
//  AppDelegate.m
//  HelloWorld
//
//  Created by XCode.md on 11/11/11.
//  Copyright (c) 2011 XCode.md. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
UILabel *helloLabel;
}
@property (strong, nonatomic) UIWindow *window;
@end

Deschidem fișierul “AppDelegate.m“, gasim metoda/functia “- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions” și inainte de “return YES” adaugam cateva randuri de cod

    //alocam variabila/obiectul helloLabel cu pozitia pe x = 120px, pozitia pe y = 160px, latimea=100px si inaltimea=60px
    helloLabel = [[UILabel alloc] initWithFrame:CGRectMake(120, 160, 100, 60)];
    //setam textul ce apare pe helloLabel
    helloLabel.text = @"Salut Lume!";
    //afisam variabila/obiectul helloLabel pe ecranul device-ului, atasind-ul ferestrei principale
    [self.window addSubview:helloLabel];

Fișierul nostru ar trebui sa arate in felul urmator

//
//  AppDelegate.m
//  HelloWorld
//
//  Created by XCode.md on 11/11/11.
//  Copyright (c) 2011 XCode.md. All rights reserved.
//
#import "AppDelegate.h"
@implementation AppDelegate

@synthesize window = _window;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //aici se creaza si aloca fereastra principala care va avea dimensiunea ecranului device-ului nostru
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // aici setam culoarea backround-ului ferestrei principale
    self.window.backgroundColor = [UIColor whiteColor];
    // indicam ca aceasta fereastra este vizibila si principala pentru interactionarea cu utilizatorul
    [self.window makeKeyAndVisible];
    //alocam variabila/obiectul helloLabel cu pozitia pe x = 120px, pozitia pe y = 160px, latimea=100px si inaltimea=60px
    helloLabel = [[UILabel alloc] initWithFrame:CGRectMake(120, 160, 100, 60)];
    //setam textul ce apare pe helloLabel
    helloLabel.text = @"Salut Lume!";
    //afisam variabila/obiectul helloLabel pe ecranul device-ului, atasind-ul ferestrei principale
    [self.window addSubview:helloLabel];
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    /*
     Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
     Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
     */
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    /*
     Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
     If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
     */
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    /*
     Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
     */
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    /*
     Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
     */
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    /*
     Called when the application is about to terminate.
     Save data if appropriate.
     See also applicationDidEnterBackground:.
     */
}

@end

Acum verificăm dacă totul merge bine si programa va rula fară erori. Apăsam butonul “Run”

 

Xcode va lansa acum Simulatorul iOS cu aplicaţia noastră în ea.

 

Pentru cei ce doresc sa descarce acest proiect am pus la dispozitie codul sursa

Leave a Reply

You must be logged in to post a comment.