Post on 09-Sep-2020
Melhore sua vida, sua interface e diga adeus à
UIAlertView
Gustavo Pelosi Ambrozio
1
Programação
• Amor e ódio pela UIAlertView
• Como os Blocks me livraram do ódio
• O ódio renasce com outra face
• Uma visão de como se livrar do ódio
• Eliminando o ódio com código
• Espalhando o amor
2
Amor e ódio pela UIAlertView
• (aparentemente) Fácil de usar
• Prático
• Delegates
• Swithes
• Tags
• Constantes
3
@interface MyViewController : UIViewController <UIAlertViewDelegate>
#define kRemoveConfirmAlertView 1#define kSendAlertView 2#define kLogoutConfirmAlertView 3
- (IBAction)sendItem:(id)sender{ UIAlertView *view = [[UIAlertView alloc] initWithTitle:@”Enviar por” message:@”Como deseja enviar ?” delegate:self cancelButtonTitle:@”Cancelar” otherButtonTitles:@”Twitter”, @”Facebook”, @”Email”, nil]; view.tag = kSendAlertView; [view show]; [view release];}
4
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
switch (alertView.tag){
case kRemoveConfirmAlertView:if (buttonIndex == 1)
[self removeItem];break;
case kSendAlertView:{
switch (buttonIndex){
case 0: // Cancelbreak;
case 1: // Twitter[self sendWith:kTwitter];break;
case 2: // Facebook[self sendWith:kFacebook];break;
case 3: // emailMFMailComposeViewController *view = [[MFMailComposeViewController alloc] init];view.mailComposeDelegate = self;[self.navigationController presentModalViewController:view animated:YES];break;
}}break;
}}
5
Como os Blocks me livraram do ódio
• PSFoundation (https://github.com/steipete/PSFoundation/tree/master/Utils)
• Peter Steinberger
• PSAlertView e PSActionSheet
• Renomeado para BlockAlertView e BlockActionSheet
6
@interface MyViewController : UIViewController <UIAlertViewDelegate>
#define kRemoveConfirmAlertView 1#define kSendAlertView 2#define kLogoutConfirmAlertView 3
- (IBAction)sendItem:(id)sender{ BlockAlertView *view = [BlocAlertView alertWithTitle:@”Enviar por” message:@”Como deseja enviar ?”]; [view setCancelButtonWithTitle:@”Cancelar” block:nil]; [view addButtonWithTitle:@”Twitter”, block:^{ [self sendWith:kTwitter]; }]; [view addButtonWithTitle:@”Facebook”, block:^{ [self sendWith:kFacebook]; }]; [view addButtonWithTitle:@”Email”, block:^{ MFMailComposeViewController *view = [[MFMailComposeViewController alloc] init];
view.mailComposeDelegate = self;[self.navigationController presentModalViewController:view animated:YES];
}]; view .tag = kSendAlertView; [view show];}
7
- (IBAction)sendItem:(id)sender{ BlockAlertView *view = [BlocAlertView alertWithTitle:@”Enviar por” message:@”Como deseja enviar ?”];
[view setCancelButtonWithTitle:@”Cancelar” block:nil];
[view addButtonWithTitle:@”Twitter”, block:^{ [self sendWith:kTwitter]; }];
[view addButtonWithTitle:@”Facebook”, block:^{ [self sendWith:kFacebook]; }];
[view addButtonWithTitle:@”Email”, block:^{ MFMailComposeViewController *view = [[MFMailComposeViewController alloc] init];
view.mailComposeDelegate = self;[self.navigationController presentModalViewController:view animated:YES];
}];
[view show];}
8
Lição número 1
1. Substitua delegates por blocks (quase) sempre
• Exceção: Quando a classe que executa a ação pode ser dispensada antes do bloco ser executado (NSURLConnection, por exemplo)
9
O ódio renasce com outra face
10
Uma visão de como se livrar do ódio
11
Lições número 2 e 3
2. Não fique preso à API da Apple
3. Não tenha preguiça de implementar do zero
12
Eliminando o ódio com código
UIWindow
UView
UILabel
UIButton
13
Lição número 4
4. UIWindow é uma classe obscura e pouco usada (e pessimamente documentada) mas que pode resolver vários problemas de interface. Use com sabedoria.
14
Eliminando o ódio
15
Espalhando o amor
• BlockAlertView e BlockActionSheets são open source
• github.com/gpambrozio/BlockAlertsAnd-ActionSheets
• blog.codecropper.com/2012/01/replicating-tweetbot-alerts-and-action-sheets/
• Melhorias à UIAlertView e UIActionSheet
16
Espalhando o amor
• Importar 6 arquivos, 3 .h e 3 .m
• Copiar um arquivo .h com definições de UI
• Copiar os assets ou criar os seus
• Alterar UI (opcional)
17
- (IBAction)sendItem:(id)sender{ BlockAlertView *view = [BlocAlertView alertWithTitle:@”Enviar por” message:@”Como deseja enviar ?”];
[view setCancelButtonWithTitle:@”Cancelar” block:nil];
[view addButtonWithTitle:@”Twitter”, block:^{ [self sendWith:kTwitter]; }];
[view addButtonWithTitle:@”Facebook”, block:^{ [self sendWith:kFacebook]; }];
[view setDestructiveButtonWithTitle:@”Apagar”, block:^{ [self removeItem]; }];
[view show];}
18
Espalhando o amor
19
Amor avançado• Fundo e botões são apenas PNGs
20
Amor avançado• É só uma UIView....
[BlockBackgroundsharedInstance]
UIView
21
Amor avançado• BlockTextPromptAlertView
22
Amor animado
23
Lição número 5
5. Abrir o código é bom: Melhora seu karma, sua reputação e até seu código.
24
Gustavo Ambrozio
• @gpambrozio
• blog.codecropper.com
• github.com/gpambrozio
• linkedin.com/in/gustavoambrozio
25