2012-10-04 20 views
5


Mam dwa UIAlertViews z przyciskami ok/cancel.
złapię odpowiedź użytkownika poprzez:Wiele UIAlertViews w tym samym widoku

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

Pytanie mam jest który alertView jest obecnie otwarty?
mam różne działania się dzieje po kliknięciu OK/Cancel na każdej z nich ...

+0

użyć właściwości .tag do odróżnienia. [Oto pytanie pytasz] [1] [1]: http://stackoverflow.com/questions/4346418/uialertviewdelegate-and-more-alert-windows –

Odpowiedz

20

Masz kilka opcji:

  • Stosować Ivars. Podczas tworzenia alertu widok:

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

    A w metodzie Delegat:

    - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { 
        if (alertView == myFirstAlertView) { 
         // do something. 
        } else if (alertView == mySecondAlertView) { 
         // do something else. 
        } 
    } 
    
  • użyć właściwości tag z 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; 
        } 
    } 
    
  • Podklasa UIAlertView i dodać obiekt userInfo. W ten sposób możesz dodać identyfikator do swoich widoków alertów.

    @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 jest UIView podklasy, dzięki czemu można używać jej tag własności do identyfikacji. Więc kiedy utworzyć alert widok ustawić jej wartość znacznika, a następnie będzie można wykonać następujące czynności:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{ 
    if (alertView.tag == kFirstAlertTag){ 
     // First alert 
    } 
    if (alertView.tag == kSecondAlertTag){ 
     // First alert 
    } 
} 
Powiązane problemy