UIAlertView Button Action Code

Does anyone know how to do actions for buttons in UIAlertview? If yes, please guide me.

+3
source share
5 answers
- (void)alertView:(UIAlertView *)alertView 
         didDismissWithButtonIndex:(NSInteger) buttonIndex 
{
    if (buttonIndex == 0)
    {
        NSLog(@"Cancel Tapped.");
    }
    else if (buttonIndex == 1) 
    {    
        NSLog(@"OK Tapped. Hello World!");
    }
}

Try this code, it will work for you ...

+8
source

When buttons are clicked in a UIAlertView its delegation method

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

called out. Your delegate should implement this method and check which button was clicked.

    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    switch (buttonIndex) {
        case 0:
            // Do something for button #1
            break;
        case 1:
            // Do something for button #2
            break;
        ...
    }
}

If you have several types of warnings, you can distinguish them by name as follows:

if ([alertView.title isEqualToString: yourAlertView.title]) {
    // proceed...
}
+4
source
+2

,

UIAlertView, :

 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
 {     
     if (buttonIndex == 0) {
        //Some Implementation
     } else if(buttonIndex == 1) {
        //Some Implementation
     }
 }
+2

UIAlerView.

UIAlertViewDelegate .

,

+1
source

All Articles