Mutiple UIAlertView handling in IOS / Objective C

Mutiple UIAlertView handling in IOS / Objective C

I am explaining about the multiple UIAlertView handling.

Below I have shown how to show a UIAlertView

UIAlertView* dialog = [[UIAlertView alloc] initWithTitle:@"This is first message box pop up!\n"
                                                             message:@"\n\n "
                                                            delegate:self
                                                   cancelButtonTitle:@"Cancel"
                                                   otherButtonTitles:@"OK", nil];
            dialog.tag = 1;
            dialog.alertViewStyle = UIAlertViewStylePlainTextInput;
            [dialog show];

UIAlertView* dialog = [[UIAlertView alloc] initWithTitle:@"This is second message box pop up!\n"
                                                             message:@"\n\n "
                                                            delegate:self
                                                   cancelButtonTitle:@"Cancel"
                                                   otherButtonTitles:@"OK", nil];
            dialog.tag = 2;
            dialog.alertViewStyle = UIAlertViewStylePlainTextInput;
            [dialog show];

I have explained it well under this article.  You can review it if you do not know how show a message box (UIAlertView) to user.

Now I am going to show you how to identify the OK button or Cancel button click of each and every alert views. 


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

    if (alertView.tag == 1) {
    //This is first message box execute method
        if (buttonIndex == 1)
        { // This is OK button click
            @try {
 NSString *useremail = [alertView textFieldAtIndex: 0].text;
            } @catch (NSException * e) {
                NSLog(@"Exception: %@", e);
            } @finally {
            }
        }else if((alertView.tag == 0) {
//this is cancel button click
}
    } else if (alertView.tag == 2) {
       
         //This is second message box execute method

    } 
    
}


Now I m going to explain you about method.
When we create a UIAlertView you can see I have used below line of code.

dialog.tag = 1; // this is for first message
dialog.tag = 2; // this is for second message

This is the way app identify the executing uialertview. And it catch by below line of alertView clickedButtonAtIndex: method.

if (alertView.tag == 1) {
}

I hope now you have how to handle the execution feed back of multiple UIAlertViews but now you may want to identify to know which button user has clicked. Either "OK" or "Cancle".

Below lines of code identify it.

if (buttonIndex == 1){
}

Enjoy with UIAlertViews. Please do not forget to put a like if you get help from my article.
Thanks.
Dasun



Comments

Popular Posts