2012-10-04 17 views
5


Ho due UIAlertViews con i pulsanti OK/Annulla.
prendo la risposta utente:Più UIAlertViews nella stessa vista

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

La domanda che sto avendo è, che alertView è aperto?
devo diverse azioni da fare quando si fa clic su OK/annullare su ciascuno ...

+0

Utilizzare la proprietà .tag a differenziarsi. [Ecco la domanda che hai posto] [1] [1]: http://stackoverflow.com/questions/4346418/uialertviewdelegate-and-more-alert-windows –

risposta

20

Sono disponibili diverse opzioni:

  • Utilizzare Ivars. Quando si crea la visualizzazione degli avvisi:

    myFirstAlertView = [[UIAlertView alloc] initWith...]; 
    [myFirstAlertView show]; 
    // similarly for the other alert view(s). 
    

    E nel metodo delegato:

    - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { 
        if (alertView == myFirstAlertView) { 
         // do something. 
        } else if (alertView == mySecondAlertView) { 
         // do something else. 
        } 
    } 
    
  • Utilizzare la tag proprietà di UIView:

    #define kFirstAlertViewTag 1 
    #define kSecondAlertViewTag 2 
    

    UIAlertView *firstAlertView = [[UIAlertView alloc] initWith...]; 
    firstAlertView.tag = kFirstAlertViewTag; 
    [firstAlertView show]; 
    // similarly for the other alert view(s). 
    

    - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { 
        switch (alertView.tag) { 
         case kFirstAlertViewTag: 
          // do something; 
          break; 
         case kSecondAlertViewTag: 
          // do something else 
          break; 
        } 
    } 
    
  • sottoclasse UIAlertView e aggiungere un alloggio userInfo. In questo modo puoi aggiungere un identificatore alle tue viste di avviso.

    @interface MyAlertView : UIAlertView 
    @property (nonatomic) id userInfo; 
    @end 
    

    myFirstAlertView = [[MyAlertView alloc] initWith...]; 
    myFirstAlertView.userInfo = firstUserInfo; 
    [myFirstAlertView show]; 
    // similarly for the other alert view(s). 
    

    - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { 
        if (alertView.userInfo == firstUserInfo) { 
         // do something. 
        } else if (alertView.userInfo == secondUserInfo) { 
         // do something else. 
        } 
    } 
    
1

UIAlertView è una sottoclasse UIView in modo da poter utilizzare la sua proprietà tag per l'identificazione. Così, quando si crea vista avviso impostare il valore di tag e poi si sarà in grado di effettuare le seguenti operazioni:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{ 
    if (alertView.tag == kFirstAlertTag){ 
     // First alert 
    } 
    if (alertView.tag == kSecondAlertTag){ 
     // First alert 
    } 
} 
Problemi correlati