Aller au contenu

Les applications en Label

Les applications en Label

Partie 1 :

Je vous explique ici comment fabriquer une petite application , avec un label et un bouton sous Xcode et comment la faire fonctionner .Le code source de cette présentation est disponible via le lien sous la petite video , il est sous forme d’un produit gratuit , donc n’hésitez pas a le télécharger .

Téléchargez le code source ici ….

Voici le code de ViewController.h

//
// ViewController.h
// Label 1
//
// Created by Sylvain Altmayer on 31/10/2015.
// Copyright © 2015 Sylvain Altmayer. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UILabel *Label;

@end

 

Voici le code dans le ViewController.m

//
// ViewController.m
// Label 1
//
// Created by Sylvain Altmayer on 31/10/2015.
// Copyright © 2015 Sylvain Altmayer. All rights reserved.
//

#import « ViewController.h »

@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

– (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
– (IBAction)push:(UIButton *)sender {_Label.text = @ »toto »;}

@end

 

Vous voyez ici comme expliqué dans la video les déclaration a utiliser pour utiliser un Label et un bouton et les faire fonctionner ensemble en espérant que ça vous donnera plein d’idées !

 

 

 

Partie 2 : Une calculatrice !

Voici la suite ! , l’idée ici est de modifier la toute petite application qui a été fabriqué dans la première partie pour en faire une petite calculatrice  la vidéo est plus longue car le travail est plus difficile que dans la première partie , j’espère que vous aurez la patience d’aller jusqu’au bout 🙂 . le code source est également disponible en téléchargement la encore sous la forme d’un petit produit gratuit ! n’hésitez pas a le télécharger !

 

 

Téléchargez le code source ici ….

 

Le ViewController.h , là encore très simple il n’y a que deux labels un pour affiner les nombres et le résultat et l’autre de test pour contrôler les données qui circulent durant le calcul. 

//
// ViewController.h
// Label 1
//
// Created by Sylvain Altmayer on 31/10/2015.
// Copyright © 2015 Sylvain Altmayer. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UILabel *Label;
@property (weak, nonatomic) IBOutlet UILabel *Label_test;

@end

 

Le ViewController.m ici plus compliqué on distingue 3 parties ici .La déclaration des variables , les fonctions pour l’affichage , le calcul et le résultat et la declaration des boutons avec leurs renvoi d’information et les  » Preform selector » qui permettent de retourner a une fonction . 

 

//
// ViewController.m
// Label 1
//
// Created by Sylvain Altmayer on 31/10/2015.
// Copyright © 2015 Sylvain Altmayer. All rights reserved.
//

#import « ViewController.h »
NSString *affichage_nb=@ » » ;
NSString *A_affichage=@ » » ;
NSString *N_affichage=@ » » ;
NSString *symbol=@ » »;
int nombre_1=0;
int nombre_2=0;
int nombre_recu=0;
int resultat=0;
int cycle=0;
int nb_symbol=0;
@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

– (void)maj_nb{
//affichage=[NSString stringWithFormat:@ »%d »,nombre_recu];
if(cycle==0){
N_affichage=[NSString stringWithFormat:@ »%d »,nombre_recu];
affichage_nb=[NSString stringWithFormat:@ »%@%@ »,A_affichage,N_affichage];

nombre_1=(nombre_1*10)+nombre_recu;
//cycle++;
_Label_test.text=[NSString stringWithFormat:@ »%d »,nombre_1];
[self performSelector:@selector(affichage_nb)];
}
if(cycle==1){_Label.text=symbol;affichage_nb=@ » »;A_affichage=@ » »;N_affichage=@ » »;}
if(cycle==2){
N_affichage=[NSString stringWithFormat:@ »%d »,nombre_recu];
affichage_nb=[NSString stringWithFormat:@ »%@%@ »,A_affichage,N_affichage];

nombre_2=(nombre_2*10)+nombre_recu;
//cycle++;
_Label_test.text=[NSString stringWithFormat:@ »%d »,nombre_2];
[self performSelector:@selector(affichage_nb)];
}
}

– (void)affichage_resultat{_Label.text=[NSString stringWithFormat:@ »%d »,resultat];cycle=0;nombre_1=0;nombre_2=0;affichage_nb=@ » »;A_affichage=@ » »;N_affichage=@ » »;}
– (void)affichage_nb{
A_affichage=affichage_nb;
_Label.text=affichage_nb;}

– (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
– (IBAction)b_1:(UIButton *)sender {nombre_recu=1;if(cycle==1){cycle=2;}[self performSelector:@selector(maj_nb)];}
– (IBAction)b_2:(UIButton *)sender {nombre_recu=2;if(cycle==1){cycle=2;}[self performSelector:@selector(maj_nb)];}
– (IBAction)b_3:(UIButton *)sender {nombre_recu=3;if(cycle==1){cycle=2;}[self performSelector:@selector(maj_nb)];}
– (IBAction)b_4:(UIButton *)sender {nombre_recu=4;if(cycle==1){cycle=2;}[self performSelector:@selector(maj_nb)];}
– (IBAction)b_5:(UIButton *)sender {nombre_recu=5;if(cycle==1){cycle=2;}[self performSelector:@selector(maj_nb)];}
– (IBAction)b_6:(UIButton *)sender {nombre_recu=6;if(cycle==1){cycle=2;}[self performSelector:@selector(maj_nb)];}
– (IBAction)b_7:(UIButton *)sender {nombre_recu=7;if(cycle==1){cycle=2;}[self performSelector:@selector(maj_nb)];}
– (IBAction)b_8:(UIButton *)sender {nombre_recu=8;if(cycle==1){cycle=2;}[self performSelector:@selector(maj_nb)];}
– (IBAction)b_9:(UIButton *)sender {nombre_recu=9;if(cycle==1){cycle=2;}[self performSelector:@selector(maj_nb)];}
– (IBAction)b_0:(UIButton *)sender {nombre_recu=0;if(cycle==1){cycle=2;}[self performSelector:@selector(maj_nb)];}
– (IBAction)b_eg:(UIButton *)sender {if(symbol==[NSString stringWithFormat:@ »X »]){resultat=nombre_1*nombre_2;[self performSelector:@selector(affichage_resultat)];}
if(symbol==[NSString stringWithFormat:@ »: »]){resultat=nombre_1/nombre_2;[self performSelector:@selector(affichage_resultat)];}
if(symbol==[NSString stringWithFormat:@ »-« ]){resultat=nombre_1-nombre_2;[self performSelector:@selector(affichage_resultat)];}
if(symbol==[NSString stringWithFormat:@ »+ »]){resultat=nombre_1+nombre_2;[self performSelector:@selector(affichage_resultat)];}}

– (IBAction)b_mu:(UIButton *)sender {symbol=[NSString stringWithFormat:@ »X »];cycle=1;[self performSelector:@selector(maj_nb)];}
– (IBAction)b_di:(UIButton *)sender {symbol=[NSString stringWithFormat:@ »: »];cycle=1;[self performSelector:@selector(maj_nb)];}
– (IBAction)b_mo:(UIButton *)sender {symbol=[NSString stringWithFormat:@ »-« ];cycle=1;[self performSelector:@selector(maj_nb)];}
– (IBAction)b_pl:(UIButton *)sender {symbol=[NSString stringWithFormat:@ »+ »];cycle=1;[self performSelector:@selector(maj_nb)];}

@end