É possível não descartar uma visão de Uialert
-
20-09-2019 - |
Pergunta
O protocolo UIALERTVIEWDELEGATE possui vários métodos opcionais, incluindo:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
Isso parece sugerir que nem todos os cliques de botão realmente descartam a exibição de alerta. No entanto, não vejo como configurar a visualização de alerta para não descartar automaticamente com nenhum botão pressionar.
Tenho que criar uma subclasse para conseguir isso?
Por que o protocolo UialertviewDelegate teria:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex;
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
E
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
Se não opcionalmente, não suporta que não descarte a exibição de alerta com cada botão, clique em?
Breve de lado: percebo para o que o Uialertview foi projetado. Mas meu objetivo é permitir que o usuário copie algum texto para a placa de pasta antes que o aplicativo saia (o que acontece automaticamente quando a visualização de alerta é descartada.
Solução
Sim. Subclasse UIAlertView
e depois sobrecarga -dismissWithClickedButtonIndex:animated:
, por exemplo
@implementation MyAlertView
-(void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated {
if (buttonIndex should not dismiss the alert)
return;
[super dismissWithClickedButtonIndex:buttonIndex animated:animated];
}
@end
Não oficialmente você pode definir um
-(void)alertSheet:(UIAlertSheet*)sheet buttonClicked:(id)button;
método para o delegado que o fará de ignorar -dismissWithClickedButtonIndex:animated:
, mas é sem documentos, então não sei se é adequado para você.
Outras dicas
willPresentAlertView:
, didPresentAlertView:
, alertView:willDismissWithButtonIndex:
, e alertView:didDismissWithButtonIndex:
são para rastrear o início e o final das animações do Uialertview.
Aplicativos que não precisam rastrear as animações do UialerTView podem simplesmente usar alertView:clickedButtonAtIndex:
. Os documentos para esse método dizem "o receptor é automaticamente julgado improcedente depois que esse método for invocado".
Na minha opinião: não há razão para manter o Alertview. Mesmo se você quiser mantê-lo, pense em "re-comparecer", mantendo uma referência, ligue para [alertview show] ==> Não há necessidade de subclasse nada. Boas notícias, hein?
AVISO
De algumas fontes, ouvi dizer que poucos aplicativos foram rejeitados após esse processo. Eu tive sorte no meu caso durante o iOS6, então estou mostrando código aqui. Use por seu próprio risco:-/
A subclasse é a melhor maneira. Crie um bool
A bandeira para alerta deve ficar ou não.
Esta é a subclasse de UIAlertView
//
// UICustomAlertView.h
//
#import <UIKit/UIKit.h>
@interface UICustomAlertView : UIAlertView
{
}
@property(nonatomic, assign) BOOL dontDisppear;
@end
//
// UICustomAlertView.m
//
#import "UICustomAlertView.h"
@implementation UICustomAlertView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
-(void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated {
if(self.dontDisppear)
return;
[super dismissWithClickedButtonIndex:buttonIndex animated:animated];
}
@end
E foi assim que eu usei no meu código
if(![txtUsername.text isEqualToString:@"admin"] && ![txtPassword.text isEqualToString:@"admin"])
{
alertLogin.dontDisppear = YES;
alertLogin.message = NSLocalizedString(@"my_alert", nil);
}
else
{
alertLogin.dontDisppear = NO;
// proceed
}
#import "MLAlertView.h"
@implementation MLAlertView
-(void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated {
}
-(void)dismissNow:(NSInteger)buttonIndex {
[super dismissWithClickedButtonIndex:buttonIndex animated:YES];
}