Pregunta

El protocolo UIAlertviewDelegate tiene varios métodos opcionales, incluyendo:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;

Esto parece sugerir que no todas las pulsaciones de botón realidad oponen a la visión de alerta. Sin embargo, no veo forma de configurar la vista de alertas para no descarta automáticamente con cualquier pulsación de botón.

¿Tengo que crear una subclase de lograr esto?

¿Por qué el Protocolo UIAlertViewDelegate tener:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex;
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex

y

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;

Si no apoyaba opcionalmente no despedir a la vista de alerta con cada botón de clic?

Aparte Breve: Me di cuenta de lo UIAlertView fue diseñado. Pero mi propósito es permitir al usuario copiar un texto a la mesa de pasta antes de que las salidas de aplicaciones (lo que ocurre automáticamente cuando se desestimó la vista de alerta.

¿Fue útil?

Solución

Sí. UIAlertView Subclase y luego sobrecargue -dismissWithClickedButtonIndex:animated:, por ejemplo.

@implementation MyAlertView 
-(void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated {
   if (buttonIndex should not dismiss the alert)
      return;
   [super dismissWithClickedButtonIndex:buttonIndex animated:animated];
}
@end

Extraoficialmente se puede definir un

-(void)alertSheet:(UIAlertSheet*)sheet buttonClicked:(id)button;

método para el delegado que hará pasar por alto -dismissWithClickedButtonIndex:animated:, pero es indocumentado , así que no sé si es adecuado para usted.

Otros consejos

willPresentAlertView:, didPresentAlertView:, alertView:willDismissWithButtonIndex: y alertView:didDismissWithButtonIndex: son para seguir el inicio y el final de las animaciones de UIAlertView.

Las aplicaciones que no necesitan para realizar un seguimiento de las animaciones de UIAlertView simplemente puede usar alertView:clickedButtonAtIndex:. La documentación para que el método dicen que "el receptor es despedido de forma automática después de que se invoca este método."

En mi opinión: No hay ninguna razón para mantener alertView. Incluso si quieres mantenerlo, sólo pensar en "re-show" que, al mantener una referencia, a continuación, llamar [mostrar alertView] ==> NO NECESITA NADA subclase . Buenas noticias, ¿eh?

  

ADVERTENCIA

     

A partir de algunas fuentes que he oído que pocos han aplicación fue rechazado   después de este proceso. Tuve suerte en mi caso durante iOS6 así que estoy   mostrando código aquí. Utilizar en su propio riesgo: - /

La subclasificación es la mejor manera. Crear una bandera bool para la alerta debe permanecer o no.

Este es el Subclase 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

Y esto es como lo he usado en mi 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];
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top