2012-04-02 11 views
6

uomini:UIAlertViewDelegate: clickedButtonAtIndex e due pulsanti

ci sono due pulsanti nella mia viewController di applicazione di test, quello giusto Io lo chiamo "NO",

e l'altro è "SI". I due pulsanti chiameranno due funzioni diverse, e quando

utente preme uno dei pulsanti, voglio mostrare all'utente un avviso per confermare che.

so usare l'UIAlertViewDelegate

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

ma ci sono due pulsanti, Sono perplesso. Come posso sapere quale tasto è premuto.

Quindi, pls aiutarmi con questo, vi ringrazio in anticipo!

risposta

17

Quando si crea un UIAlertView voi può impostare un tag per esso

-(IBAction)yesButtonClick:(id)sender{ 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Message" delegate:self cancelButtonTitle: @"Cancel" otherButtonTitles:@"OK", nil]; 
    alert.tag = 101; 
    [alert show]; 
} 

-(IBAction)noButtonClick:(id)sender{ 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Message" delegate:self cancelButtonTitle: @"Cancel" otherButtonTitles:@"OK", nil]; 
    alert.tag = 102; 
    [alert show]; 
} 

Nella controllo metodo delegato che avviso viene mostrato

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
    if (alertView.tag == 101) { 
     // from YES button 
    } 
    else if (alertView.tag == 102) { 
     // from NO button 
    } 
} 
+0

Grazie! Che funzioni ! Buona giornata ! – jxdwinter

0
- (void)alertView:(UIAlertView *)actionSheet 
    clickedButtonAtIndex:(NSInteger)buttonIndex{ 
    switch(buttonIndex){ 
    case 0: 
     //YES button handler 
     break; 
    case 1: 
     //NO button handler 
     break; 
    default: 
     break; 
    } 
} 
+0

Credo che vuole identificare ciò che il pulsante mostra questo avviso .. – beryllium

+0

@beryllium: sì, anche me pensa la stessa –

+0

Sì, voglio sapere è il "SI" oppure "NO" tasto premuto, e quindi assicurarsi che l'utente vuole andare avanti, grazie per le vostre risposte! – jxdwinter

0

è possibile utilizzare l'attributo tag per fare la differenza tra il rimorchio UIAlertView
nella funzione del tasto 1
alertView1.tag=1;
e in

-(void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
if(actionSheet.tag==1){ 
//first button was clicked 
} 

} 
Problemi correlati