Applications mobiles

Transcription

Applications mobiles
Applications mobiles
Partie 4
Gestion des UIView : ToolBar, TabBar et
NavBar
Gestion des UIView
• Terminaux mobiles : une seule fenêtre
 Organisation des interfaces par « vue »
• Création des UIView via IB
• MVC  UIViewController
– Permet d’exploiter plusieurs UIView
Des ViewControllers particuliers
• Cocoa propose plusieurs ViewControllers pour
organiser l’interface
– Toolbar : Barre de commande (cf Safari)
– TabBar : Présentation modale (cf iPod)
– NavBar : Navigation hiérarchique (cf Setting)
• Peuvent être mélangés (cf Mail)
• Utilisent la délégation !
Exemple : ColorNavigation
• Créez un nouveau projet
« ColorNavigation » de type NavigationBased
ColorNavigation
• Une appli qui sert à rien !
• Mais illustre l’utilisation de
– UINavigationController
– UITableView
– Délégation
• But : Affichage et édition d’une liste de
couleurs
ColorNavigation
NSMutableArray
UIColor
CGColor
rand()
UITableViewControll
er
UINavigationControll
er
UIViewController
UIPickerView
UIPickerViewDelegate
UIPickerViewDataSour
ce
UIButton
Animations
RootViewController.h
@interface RootViewController : UITableViewController {
int nbColors;
NSMutableArray *colorArray;
}
@end
 Beaucoup de méthodes sont déjà implémentées dans
RootViewController.m !
RootViewController.m
• Initialisation aléatoire des couleurs
:
- (void)viewDidLoad {
[super viewDidLoad];
nbColors = 1000;
colorArray = [[NSMutableArray alloc] initWithCapacity:nbColors];
srand(time(NULL));
for (int i=0; i<nbColors; i++) {
float r = (float)((float)rand()/(float)RAND_MAX);
float g = (float)((float)rand()/(float)RAND_MAX);
float b = (float)((float)rand()/(float)RAND_MAX);
[colorArray addObject:[UIColor colorWithRed:r green:g blue:b alpha:1.0]];
}
// Uncomment the following line to display an Edit button in the navigation
bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
RootViewController.m
• Gestion d’une
UITableView :
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:
(NSInteger)section {
return nbColors;
}
RootViewController.m
• Gestion d’une
UITableView :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath {
[...]
// Configure the cell.
UIView* backgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
[backgroundView setBackgroundColor:[colorArray objectAtIndex:indexPath.row]];
[cell setBackgroundView:backgroundView];
return cell;
}
- (CGFloat)tableView:(UITableView *)tv heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 10.0f;
}
MyColorViewController
• Créez un nouvel objet nommé
« MyColorViewController » qui hérite de
UIViewController
– Menu File / New File…
– Créer en même temps le xib associé
MyColorViewController.xib et .h
• Créez le xib dans IB
• Ajoutez la définition des Outlets
dans le .h
• Effectuez les connections
MyColorViewController.h
• Ajout d’un nouveau constructeur avec un
UIColor et un int …
@interface MyColorViewController : UIViewController {
IBOutlet UILabel *redLabel;
IBOutlet UILabel *greenLabel;
IBOutlet UILabel *blueLabel;
int num;
UIColor *color;
}
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle
*)nibBundleOrNil color:(UIColor*)aColor num:(int)aNum;
@end
MyColorViewController.m
• Ajout d’un nouveau constructeur avec un
UIColor et un int …
// The designated initializer. Override if you create the controller
programmatically and want to perform customization that is not appropriate for
viewDidLoad.
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle
*)nibBundleOrNil color:(UIColor*)aColor num:(int)aNum {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
color = [aColor retain];
num = aNum;
}
return self;
}
MyColorViewController.m
• Mise en forme de la UIView et des UIObjets…
// Implement viewDidLoad to do additional setup after loading the view, typically from a
nib.
- (void)viewDidLoad {
[super viewDidLoad];
[[self view] setBackgroundColor:color];
[redLabel setText:[NSString stringWithFormat:@" Red = %f",
CGColorGetComponents([color CGColor])[0]]];
[redLabel setTextColor:[UIColor redColor]];
[greenLabel setText:[NSString stringWithFormat:@" Green = %f",
CGColorGetComponents([color CGColor])[1]]];
[greenLabel setTextColor:[UIColor greenColor]];
[blueLabel setText:[NSString stringWithFormat:@" Blue = %f",
CGColorGetComponents([color CGColor])[2]]];
[blueLabel setTextColor:[UIColor blueColor]];
}
RootViewController.m
• Gestion de la sélection d’une ligne dans le
UITableView
// Override to support row selection in the table view.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here -- for example, create and push another view controller.
MyColorViewController *colorViewController = [[MyColorViewController alloc]
initWithNibName:@"MyColorViewController" bundle:nil
color:[colorArray objectAtIndex:indexPath.row] num:indexPath.row];
[colorViewController setTitle:[NSString stringWithFormat:@"Color %d", indexPath.row]];
[self.navigationController pushViewController:colorViewController animated:YES];
[colorViewController release];
}
ColorNavigation
• Testez !
• « Build and Run »
Exercice
• Ajout et gestion du UIPickerView
– Initialisation des valeurs en fonction de la
couleur sélectionnée
– Modification de la couleur par interaction avec
le UIPickerView
– Notification du changement de couleur vers
RootViewController
Notifications
• Emettre une notification (sans objets attachés) :
[[NSNotificationCenter defaultCenter] postNotificationName:@"kMyNotif" object:nil
userInfo:nil];
• Emettre une notification (avec objets attachés) :
[[NSNotificationCenter defaultCenter] postNotificationName:@"kMyNotif" object:nil
userInfo:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:object1,
object2, nil] forKeys:[NSArray arrayWithObjects:@"O1", @"O2", nil]]];
Notifications
• Observer une notification :
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myFunction:)
name:@"kMyNotif" object:nil];
• Implémentation de la fonction associée :
- (void)myFunction:(NSNotification*) notification {
NSObject *object1 = [[notification userInfo] objectForKey:@"o1"];
NSObject *object2 = [[notification userInfo] objectForKey:@"o2"];
}

Documents pareils